Hướng dẫn dùng mysql w3schools trong PHP
With PHP, you can connect to and manipulate databases. Show
MySQL is the most popular database system used with PHP. What is MySQL?
The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows. Databases are useful for storing information categorically. A company may have a database with the following tables:
PHP + MySQL Database System
Database QueriesA query is a question or a request. We can query a database for specific information and have a recordset returned. Look at the following query (using standard SQL): SELECT LastName FROM Employees The query above selects all the data in the "LastName" column from the "Employees" table. To learn more about SQL, please visit our SQL tutorial. Download MySQL DatabaseIf you don't have a PHP server with a MySQL Database, you can download it for free here: http://www.mysql.com Facts About MySQL DatabaseMySQL is the de-facto standard database system for web sites with HUGE volumes of both data and end-users (like Facebook, Twitter, and Wikipedia). Another great thing about MySQL is that it can be scaled down to support embedded database applications. Look at http://www.mysql.com/customers/ for an overview of companies using MySQL. Select Data From a MySQL DatabaseThe SELECT statement is used to select data from one or more tables: SELECT column_name(s) FROM table_name or we can use the * character to select ALL columns from a table: To learn more about SQL, please visit our SQL tutorial. Select Data With MySQLiThe following example selects the id, firstname and lastname columns from the MyGuests table and displays it on the page: Example (MySQLi Object-oriented)$servername = "localhost"; // Create connection $sql = "SELECT id, firstname,
lastname FROM MyGuests"; if ($result->num_rows > 0) { Run example » Code lines to explain from the example above: First, we set up an SQL query that selects the id, firstname and lastname columns from the MyGuests table. The next line of code runs the query and puts the resulting data into a variable called $result. Then, the function If there are more than zero
rows returned, the function The following example shows the same as the example above, in the MySQLi procedural way: Example (MySQLi Procedural)$servername = "localhost"; // Create
connection $sql = "SELECT id, firstname, lastname FROM MyGuests"; if (mysqli_num_rows($result) > 0) { mysqli_close($conn); Run example » You can also put the result in an HTML table: Example (MySQLi Object-oriented)$servername = "localhost"; // Create connection $sql = "SELECT id, firstname, lastname FROM MyGuests"; if ($result->num_rows > 0) {
} else { echo "0 results"; } $conn->close(); ?> Run example » Select Data With PDO (+ Prepared Statements)The following example uses prepared statements. It selects the id, firstname and lastname columns from the MyGuests table and displays it in an HTML table: Example (PDO)echo "
?> Run example » |