PHP MySQL Read Data

Source: Internet
Author: User

PhpMySQL Read DataReading data from MySQL database

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

SELECT  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 a connection$conn=NewMysqli ($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 Results";}$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 a 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 Results";} 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>"; classTableRowsextendsRecursiveiteratoriterator {function__construct ($it) {Parent:: __construct ($it, Self::leaves_only); }     function  Current() {        return"<td style= ' width:150px;border:1px solid black; ' > ". Parent:: Current(). "</td>"; }     functionBeginchildren () {Echo"<tr>"; }      functionEndchildren () {Echo"</tr>". "\ n"; } }  $servername= "localhost";$username= "username";$password= "Password";$dbname= "Mydbpdo"; Try {    $conn=NewPDO ("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 result set to associative array    $result=$stmt->setfetchmode (PDO::FETCH_ASSOC); foreach(NewTableRows (NewRecursivearrayiterator ($stmt->fetchall ())) as $k=$v) {         Echo $v; }}Catch(pdoexception$e) {    Echo"Error:".$e-getMessage ();}$conn=NULL;Echo"</table>";?>

PHP MySQL Read Data

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.