How to read data from PHP MySQL

Source: Internet
Author: User
Tags sql tutorial mysql in php mysql

Reading data is important for PHP, and this article explains how to read data from PHP MySQL in detail.

Reading data from MySQL database

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

SELECT column_name (s) from table_name

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

SELECT * FROM table_name

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

Using mysqli

In the following example we read the IDs, FirstName and LastName columns of data from the Myguests table of the MyDB database and display them on the page:

Example (Mysqli-object-oriented)

<?php$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "MyDB"; Create connection $conn = new Mysqli ($servername, $username, $password, $dbname);//Check connectionif ($conn->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"]. "<br>";    }} else {    echo "0 result";} $conn->close ();? >


The above code is parsed as follows:

First, we set the SQL statement to read the IDs from the Myguests data table, FirstName and LastName three fields. We then use the Change SQL statement to fetch the result set from the database and assign it to the variable $result.

The function num_rows () determines the returned data.

If more than one data is returned, the function Fetch_assoc () puts the binding set into the associative array and loops the output. while () loops out the result set and outputs the ID, FirstName, and LastName three field values.

The following instances use the MYSQLI process-oriented approach, which resembles the above code:

Example (mysqli-process-oriented)

<?php$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "MyDB"; Create Connection $conn = Mysqli_connect ($servername, $username, $password, $dbname);//Check connectionif (! $conn) {die    ("Connection failed : " . Mysqli_connect_error ());} $sql = "SELECT ID, FirstName, LastName from myguests"; $result = Mysqli_query ($conn, $sql); if (Mysqli_num_rows ($result) > 0) {    //output data while    ($row = Mysqli_fetch_assoc ($result)) {        echo "ID:". $row ["id"]. "-Name:". $row ["FirstName"]. " " . $row ["LastName"]. "<br>";    }} else {    echo "0 result";} mysqli_close ($conn);? >


Using PDO (+ preprocessing)

The following instance uses a preprocessing statement.

The IDs, FirstName, and LastName fields in the Myguests table are selected and placed in the HTML table:

Instance (PDO)

<?phpecho "<table style= ' border:solid 1px black; ' > "echo" <tr><th>id</th><th>firstname</th><th>lastname</th></tr > "; Class TableRows extends Recursiveiteratoriterator {function construct ($it) {parent::construct ($it, Self::leav     ES_ONLY); } function current () {return ' <td style= ' width:150px;border:1px solid black; ' > ". Parent::current ().    "</td>";     } function Beginchildren () {echo "<tr>"; } function Endchildren () {echo "</tr>".    "\ n"; }} $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "Mydbpdo";    try {$conn = new PDO ("mysql:host= $servername;d bname= $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 associative array $result = $stmt->setfetchmode (PDO::FETCH_ASSOC);    foreach (New TableRows ($stmt->fetchall ())) as $k = + $v) {echo $v; }}catch (pdoexception $e) {echo "Error:". $e->getmessage ();} $conn = Null;echo "</table>";? >

This article details the operation of reading data through PHP MySQL, more learning materials to pay attention to the PHP Chinese network can be viewed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.