Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP MySQL Read Data

PHP MySQL Read Data

Read Data from MySQL Database

The SELECT statement is used to read data from a data table:



SELECT column_name(s) FROM table_name

We can use the * sign to read all fields in the data table:



SELECT * FROM table_name

To learn more about SQL, please visit our SQL tutorial.


Using MySQLi

In the example below, we read the data of the id, firstname, and lastname columns from the MyGuests table in the myDB database and display them on the page:

Example (MySQLi - Object-Oriented)

connect_error ) { die ( " Connection failed: " . $conn -> connect_error ) ; } $sql = " SELECT id, firstname, lastname FROM MyGuests " ; $result = $conn -> query ( $sql ) ; if ( $result -> num_rows > 0 ) { // output data while ( $row = $result -> fetch_assoc ( ) ) { echo " id: " . $row [ " id " ] . " - Name: " . $row [ " firstname " ] . " " . $row [ " lastname " ] . "
" ; } } else { echo " 0 results " ; } $conn -> close ( ) ; ?>

The code above is analyzed as follows:

First, we set the SQL statement to read the three fields id, firstname, and lastname from the MyGuests table. Then we use this SQL statement to fetch the result set from the database and assign it to the variable $result.

The num_rows() function checks the returned data.

If multiple rows are returned, the fetch_assoc() function puts the result set into an associative array and outputs it in a loop. The while() loop outputs the result set and prints the values of the three fields id, firstname, and lastname.

The following example uses the procedural style of MySQLi, with an effect similar to the code above:

Example (MySQLi - Procedural)

0 ) { // output data while ( $row = mysqli_fetch_assoc ( $result ) ) { echo " id: " . $row [ " id " ] . " - Name: " . $row [ " firstname " ] . " " . $row [ " lastname " ] . "
" ; } } else { echo " 0 results " ; } mysqli_close ( $conn ) ; ?>

Using PDO (+ Prepared Statements)

The following example uses a prepared statement.

Selected the id, firstname, and lastname fields from the MyGuests table and put them into an HTML table:

Example (PDO)

" ; echo " IdFirstnameLastname " ; class TableRows extends RecursiveIteratorIterator { function __construct ( $it ) { parent :: __construct ( $it , self :: LEAVES_ONLY ) ; } function current ( ) { return " " . parent :: current ( ) . " " ; } function beginChildren ( ) { echo " " ; } function endChildren ( ) { echo " " . " \n " ; } } $servername = " localhost " ; $username = " username " ; $password = " password " ; $dbname = " myDBPDO " ; try { $conn = new PDO ( " mysql:host= $servername ;dbname= $dbname " , $username , $password ) ; $conn -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION ) ; $stmt = $conn -> prepare ( " SELECT id, firstname, lastname FROM MyGuests " ) ; $stmt -> execute ( ) ; // set the result set to an associative array $result = $stmt -> setFetchMode ( PDO :: FETCH_ASSOC ) ; foreach ( new TableRows ( new RecursiveArrayIterator ( $stmt -> fetchAll ( ) ) ) as $k => $v ) { echo $v ; } } catch ( PDOException $e ) { echo " Error: " . $e -> getMessage ( ) ; } $conn = null ; echo " " ; ?>