Dr Andres Baravalle
A database is an organized collection of data. The data is typically organized to model relevant aspects of reality (for example, the availability of rooms in hotels), in a way that supports processes requiring this information (for example, finding a hotel with vacancies).
Wikipedia
A database typically is used to provide long-term storage for your information. If your web application or web server failes, a database will not loose your information.
Your information will be lost in case of disk failure - that's why redundancy is important.
To actually work with databases and tables, you use SQL statements.
Common statements include:
In the next slides we will look at the most common SQL statements. We'll see a simplified overview of SQL statements; for the full documentation refer to the MySQL web site.
SELECT select_expr
[FROM table_references
[WHERE where_condition]
[ORDER BY {col_name | expr | position}
[LIMIT {[offset,] row_count]
Example:
SELECT * FROM t1
INSERT
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE}
Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3)
UPDATE table_reference
SET col_name1={expr1|DEFAULT}
[WHERE where_condition]
Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3)
DELETE FROM tbl_name
[WHERE where_condition]
phpMyAdmin can be used to perform the most common database operations, including:
You shouldn't normally write your SQL code directly into your PHP files. Test the code in phpMyAdmin first!
<?php
// create a mysqli object, for the connection
$mysqli = new mysqli("localhost", "my_username", "my_password", "my_database");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
}
else {
// run a query and fetch the result set
if ($result = $mysqli->query("SELECT * FROM City") {
while($row = $result->fetch_array())
{
$row = $result->fetch_array();
var_dump($row);
}
$result->close();
}
$mysqli->close();
}
?>
<?php
// create a mysqli object, for the connection
$mysqli = new mysqli("localhost", "my_username", "my_password", "my_database");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
}
else {
// run your command
$mysqli->query("INSERT INTO cities VALUES (1, 'London');";
$mysqli->close();
}
?>