07 Php Mysql
07 Php Mysql
Taif University
College of Computers and Information Technology
Objectives
Unfortunately, there are many different versions. But, they must support the same
major keywords in a similar manner such as SELECT, UPDATE, DELETE,
INSERT, WHERE, etc.
Most of the SQL database programs also have their own proprietary extensions!
Here at Taif University...
The table above contains three records (one for each person) and four columns
(LastName, FirstName, Address, and City).
SQL Queries
With SQL, you can query a database and have a result set returned.
LastName
Hansen
Svendson
Pettersen
The mySQL database system requires a semicolon at the
end of the SQL statement!
SQL Data Languages
The query and update commands together form the Data Manipulation
Language (DML) part of SQL:
• SELECT - extracts data from a database table
• UPDATE - updates data in a database table
• DELETE - deletes data from a database table
• INSERT INTO - inserts new data into a database table
The Data Definition Language (DDL) part of SQL permits database tables to be
created or deleted:
• CREATE TABLE - creates a new database table
• ALTER TABLE - alters (changes) a database table
• DROP TABLE - deletes a database table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
From here you can create, modify, and drop tables, and modify the data in your
tables.
But first, you must specify which database on the server you want to use (you have
only
one,mysql>
however).
use studentID;
Database changed
Technical note
● You probably don’t need to worry about this, but thought I would
mention it here…
(Ignore this if you don’t understand it for now, or are not consulting other
references.)
Creating a Table
You can create a table you might use for the upcoming project. For example,
*If the server gives you a big ERROR, just try again from the top!
Viewing The Table Structure
Use DESCRIBE to see the structure of a table
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| num | int(11) | NO | PRI | NULL | auto_increment |
| f_name | varchar(48) | YES | | NULL | |
| l_name | varchar(48) | YES | | NULL | |
| student_id | int(11) | YES | | NULL | |
| email | varchar(48) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
Inserting Data
Using INSERT INTO you can insert a new row into your table. For example,
+-----+---------+--------+------------+----------------------+
| num | f_name | l_name | student_id | email |
+-----+---------+--------+------------+----------------------+
| 1 | Salah| Othman| 396640 | salah@tu.edu.sa |
+-----+---------+--------+------------+----------------------+
1 row in set (0.00 sec)
Inserting Some More Data
You can repeat inserting until all data is entered into the table.
● The SELECT command is the main way of getting data out of a table, or set of tables.
SELECT * FROM students;
Here the asterisk means to select (i.e. return the information in) all columns.
You can specify one or more columns of data that you want, such as
SELECT f_name,l_name FROM students;
+---------+--------+
| f_name | l_name |
+---------+--------+
| Salah | Othman |
| Muhammad| Abdullah|
+---------+--------+
2 rows in set (0.00 sec)
Getting Data Out of the Table (cont.)
You can specify other information that you want in the query using the WHERE
clause.
SELECT * FROM students WHERE l_name=‘Othman’;
+-----+---------+--------+------------+----------------------+
| num | f_name | l_name | student_id | email |
+-----+---------+--------+------------+----------------------+
| 2 | Salah | Othman | 396640 | salah@tu.edu.sa |
+-----+---------+--------+------------+----------------------+
1 row in set (0.00 sec)
...
The Final Table (cont.)
. . .
+-----+---------+----------+------------+----------------------------+------------+------+
+-----+---------+----------+------------+----------------------------+------------+------+
+-----+---------+----------+------------+----------------------------+------------+------+
mysql> exit
Bye
Other SQL Commands
● SHOW tables; gives a list of tables that have been defined in the database
● ALTER TABLE students DROP email; would drop the “email” column from
all records
● DROP TABLE students; deletes the entire “students” table, and its definition
(use the DROP command with extreme care!!)
● DELETE FROM students; removes all rows from the “students” table (so
once again, use the DELETE command with great caution), the table
definition remains to be used again
● A more useful command is something like
DELETE FROM students WHERE (num > 5) AND (num <= 10);
which selectively deletes students based on their “num” values (for example).
stores information about the “books” and “clients” tables in the “databasename”
database.
Putting Content into Your Database with PHP
We can simply use PHP functions and mySQL queries together:
• Connect to the database server and login (this is the PHP command to do so)
mysqli_connect("host","username","password");
• Close the connection to the database server (to ensure the information is stored properly)
mysql_close();
Student Database: data_in.php
<html><head><title>Putting Data in the DB</title></head>
<body>
<?php /*insert students into DB*/
if(isset($_POST["submit"])) {
$db = mysql_connect("mysql", “student1");
mysql_select_db(“student1");
Send an SQL query to the server to select data from the database into an array
$result=mysql_query(“select……");
Either, look into a row and a fieldname
$num=mysql_numrows($result);
$variable=mysql_result($result,5,“gr");
Or, fetch rows one by one
$row=mysql_fetch_array($result);