Next we are going to create a database table and fill it with data.
<?phpIn the above code example, we create a Cars table with 8 rows.
$host = "localhost";
$user = "user12";
$pass = "34klq*";
$db = "mydb";
function execute_query($query) {
$r = mysql_query($query);
if (!$r) {
echo "Cannot execute query: $query\n";
trigger_error(mysql_error());
} else {
echo "Query: $query executed\n";
}
}
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}
$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Database selected\n";
}
$query = "DROP TABLE IF EXISTS Cars";
execute_query($query);
$query = "CREATE TABLE Cars(Id INT PRIMARY KEY, Name TEXT,
Price INT) ENGINE=InnoDB";
execute_query($query);
$query = "INSERT INTO Cars VALUES(1,'Audi',52642)";
execute_query($query);
$query = "INSERT INTO Cars VALUES(2,'Mercedes',57127)";
execute_query($query);
$query = "INSERT INTO Cars VALUES(3,'Skoda',9000)";
execute_query($query);
$query = "INSERT INTO Cars VALUES(4,'Volvo',29000)";
execute_query($query);
$query = "INSERT INTO Cars VALUES(5,'Bentley',350000)";
execute_query($query);
$query = "INSERT INTO Cars VALUES(6,'Citroen',21000)";
execute_query($query);
$query = "INSERT INTO Cars VALUES(7,'Hummer',41400)";
execute_query($query);
$query = "INSERT INTO Cars VALUES(8,'Volkswagen',21600)";
execute_query($query);
mysql_close();
?>
function execute_query($query) {We have created a custom execute_query() function which will be called for each INSERT statement.
$r = mysql_query($query);
if (!$r) {
echo "Cannot execute query: $query\n";
trigger_error(mysql_error());
} else {
echo "Query: $query executed\n";
}
}
$r2 = mysql_select_db($db);Before we can work with database tables, we must select a database. A database is selected with a mysql_select_db() function.
if (!$r2) {Error handling for the database selection process.
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Database selected\n";
}
$query = "DROP TABLE IF EXISTS Cars";The first query drops a Cars table, if it already exists.
execute_query($query);
$query = "CREATE TABLE Cars(Id INT PRIMARY KEY, Name TEXT,This is the SQL statement to create the Cars table.
Price INT) ENGINE=InnoDB";
execute_query($query);
$query = "INSERT INTO Cars VALUES(1,'Audi',52642)";A car is inserted into the table.
execute_query($query);
if (!$ok) {In case of an error, we print the error message and terminate the script.
echo mysql_error();
die("Cannot execute query. \n");
}
$ php create_fill.phpExecuting the create_fill.php script.
Connection established
Database selected
Query: DROP TABLE IF EXISTS Cars executed
Query: CREATE TABLE Cars(Id INT PRIMARY KEY, Name TEXT,
Price INT) ENGINE=InnoDB executed
Query: INSERT INTO Cars VALUES(1,'Audi',52642) executed
Query: INSERT INTO Cars VALUES(2,'Mercedes',57127) executed
Query: INSERT INTO Cars VALUES(3,'Skoda',9000) executed
Query: INSERT INTO Cars VALUES(4,'Volvo',29000) executed
Query: INSERT INTO Cars VALUES(5,'Bentley',350000) executed
Query: INSERT INTO Cars VALUES(6,'Citroen',21000) executed
Query: INSERT INTO Cars VALUES(7,'Hummer',41400) executed
Query: INSERT INTO Cars VALUES(8,'Volkswagen',21600) executed
mysql> SELECT * FROM Cars;The data inserted into the Cars table.
+----+------------+--------+
| Id | Name | Price |
+----+------------+--------+
| 1 | Audi | 52642 |
| 2 | Mercedes | 57127 |
| 3 | Skoda | 9000 |
| 4 | Volvo | 29000 |
| 5 | Bentley | 350000 |
| 6 | Citroen | 21000 |
| 7 | Hummer | 41400 |
| 8 | Volkswagen | 21600 |
+----+------------+--------+
8 rows in set (0.00 sec)
No comments:
Post a Comment