English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use PHP to read records from a MySQL table.
So far, you have learned how to create a database and table and how to insert data. Now it's time to retrieve the data inserted in the previous tutorial. SQL SELECTThe statement is used to read records from the database table. Its basic syntax is as follows:
SELECT column1_name, column2_name, columnN_name FROM table_name;
Let's use the SELECT statement to perform an SQL query, and then we will execute this SQL query by passing it to the PHP mysqli_query() function to retrieve table data.
Please see ourpersonsThe database table has the following records:
+----+------------+-----------+----------------------+ | id | first_name | last_name | email | +----+------------+-----------+----------------------+ |}} 1 | Peter | Parker | [email protected] | |}} 2 | John | Rambo | [email protected] | |}} 3 | Clark | Kent | [email protected] | |}} 4 | John | Carter | [email protected] | |}} 5 | Harry | Potter | [email protected] | +----+------------+-----------+----------------------+
The following PHP code in the example selects all data stored in the persons table (using the asterisk character (*) instead of column names to select all data in the table).
<?php /*Attempt to connect to the MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without a password) */ $link = mysqli_connect("localhost", "root", "", "demo"); //Check the connection if($link === false){ die("Error: Unable to connect. " . mysqli_connect_error()); } //Attempt to execute the select query n $sql = "SELECT * FROM persons" if($result = mysqli_query($link, $sql)){ if(mysqli_num_rows($result) > 0){ echo "<table>" echo "<tr>" echo "<th>id<"/th>" echo "<th>first_name<"/th>" echo "<th>last_name<"/th>" echo "<th>email<"/th>" echo "<"/tr>" while($row = mysqli_fetch_array($result)){ echo "<tr>" echo "<td>" . $row['id'] . "<"/td>" echo "<td>" . $row['first_name'] . "<"/td>" echo "<td>" . $row['last_name'] . "<"/td>" echo "<td>" . $row['email'] . "<"/td>" echo "<"/tr>" } echo "<"/table>" // Release the result set mysqli_free_result($result); } else { echo "No records found matching your query."; } } else { echo "Error: Unable to execute $sql. " . mysqli_error($link); } //Close the connection mysqli_close($link); ?>
<?php /*Attempt to connect to the MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without a password) */ $mysqli = new mysqli("localhost", "root", "", "demo"); //Check the connection if($mysqli === false){ die("Error: Unable to connect. " . $mysqli->connect_error); } //Attempt to select query execution $sql = "SELECT * FROM persons" if($result = $mysqli->query($sql)){ if($result->num_rows > 0){ echo "<table>" echo "<tr>" echo "<th>id<"/th>" echo "<th>first_name<"/th>" echo "<th>last_name<"/th>" echo "<th>email<"/th>" echo "<"/tr>" while($row = $result->fetch_array()){ echo "<tr>" echo "<td>" . $row['id'] . "<"/td>" echo "<td>" . $row['first_name'] . "<"/td>" echo "<td>" . $row['last_name'] . "<"/td>" echo "<td>" . $row['email'] . "<"/td>" echo "<"/tr>" } echo "<"/table>" //Release the result set $result->free(); } else { echo "No records found matching your query."; } } else { echo "Error: Unable to execute $sql. " . $mysqli->error; } //Close the connection $mysqli->close(); ?>
<?php /*Attempt to connect to the MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without a password) */ try{ $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", \ //Set the PDO error mode to exception $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }); catch(PDOException $e){ die("Error: Unable to connect. ". $e->getMessage()); } //Attempt to select query execution try{ $sql = "SELECT * FROM persons" $result = $pdo->query($sql); if($result->rowCount() > 0){ echo "<table>" echo "<tr>" echo "<th>id<"/th>" echo "<th>first_name<"/th>" echo "<th>last_name<"/th>" echo "<th>email<"/th>" echo "<"/tr>" while($row = $result->fetch()){ echo "<tr>" echo "<td>" . $row['id'] . "<"/td>" echo "<td>" . $row['first_name'] . "<"/td>" echo "<td>" . $row['last_name'] . "<"/td>" echo "<td>" . $row['email'] . "<"/td>" echo "<"/tr>" } echo "<"/table>" //Release the result set unset($result); } else { echo "No records found matching your query."; } }); catch(PDOException $e){ die("Error: Unable to execute $sql. ". $e->getMessage()); } //Close the connection unset($pdo); ?>
In the above example, the data returned by the mysqli_query() function is stored in the $result variable. Each call to mysqli_fetch_array() returns the next row in the result set as an array.while loopto search all rows in the result set. Finally, you can pass a field index or field name to the $row variable (for example $row['id'] or $row[0], $row['first_name'] or $row[1, $row['last_name'] or $row[2, $row['email'] or $row[3to access the value of a single field from a row.
If you want to usefor loopThe value of the $result variable can be passed to the mysqli_num_rows() function to obtain the loop counter value or the number of rows returned by the query. This loop counter value determines how many times the loop should run.