Examples of PHP data access, examples of PHP data _php tutorial

Source: Internet
Author: User
Tags learn php learn php programming

Examples of PHP data access, examples of PHP data to explain


The example of this article for you to share two ways to access PHP data , we can compare, analyze the similarities and differences between the two ways, and finally provide a small exercise, the specific content as follows

Way One: obsolete, only do understand

1. Making a connection (establishing a channel)

$db =mysql_connect ("localhost", "root", "123"); In parentheses are "server address", "User name", "Password"

2. Select which database to operate

mysql_select_db ("MyDB", "$db");

3. Write SQL statements

$sql = "SELECT * from Info";

4. Execute SQL statements

$result =mysql_query ($sql); Query has the meaning of inquiry

5. Fetching data from the result set ($result)

$row =mysql_fetch_row ($result);  Reads one row of data $row1=mysql_fentch_row ($result) per execution;  Executes the second data var_dump ($row);//Read all data with a loop: while ($row =mysql_fetch_row ($result)) {var_dump ($row); }

method Two: Object-oriented

1. Create a Connection object:

$db =new mysqli ("localhost", "root", "123", "mydb")//in parentheses the contents are "server address", "username", "password", "Database name"

2. Determine if the connection is faulty:

2.1 Mysqli_connect_error (); Error on behalf of connection

2.2

if (Mysqli_connect_erroe ())

{

echo "Connection failed!";

Exit (); Exit program

}

2.3!mysqli_connect_error or Die ("Connection failed! "); "or" Before the connection is correct, which means the connection failed

3. Write the SQL statement:

$sql = "SELECT * From Nation";

4. Execute SQL statement: If the execution succeeds in returning the result set object, if the execution fails returns false

$result = $db->query ($sql);

5. Read the data from the result set, first determine if there is any data

if ($result) {//Returns an indexed array of rows of data, each execution returns a data Var_dump ($result->fetch_row ());  while ($row = $result->fetch_row) {var_dump ($row);   }//Returns an associative array of rows of data, with each execution returning a data var_dump ($result->fetch_row ());  Returns all data Var_dump ($result->fetch_all ()) through a two-dimensional array; Returns a row of data as an object Var_dump ($result->fetch_object ());}

Practice:

1. The form of the following drop-down menu displays the Nation table on the page

$db =new mysqli ("localhost", "root", "" "," MyDB ");! Mysqli_connection_erroe () or Die ("Connection failed!  $sql = "Select*from Nation", $result = $db->query ($sql), if ($result) {$att = $result->fetch_all (); echo ""; foreach ($att as $value) {echo '{$value [1]} "; } echo "";}

2. Find out the Info table and display it in tabular form

$db =new mysqli ("localhost", "root", "" "," MyDB ");! Mysqli_connecton_error () or Die ("Connection failed! $sql = "SELECT * from Info", $result = $bd->query ($sql), if ($result) {$att = $result->fetch_all (); echo "
 
 
  
  "; Echo"
  
  "; foreach ($att as $value) {echo" 
   ";} echo " 
  
Code Name Gender National Birthday
{$value [0]} {$value [1]} {$value [2]} {$value [3]} {$value [4]}
";}  You can also use the For loop if ($result) {$arr = $result->fetch_all (); echo "      "; echo " "; for ($i =0; $i                          "; } echo "
Code Name Sex Nation Birthday
{$arr [$i][0]}{$arr [$i][1]}{$arr [$i][2]}{$arr [$i][3]}{$arr [$i][4]}
";}</tr>

The above is the whole content of this article, I hope that you learn PHP programming help.

http://www.bkjia.com/PHPjc/1126073.html www.bkjia.com true http://www.bkjia.com/PHPjc/1126073.html techarticle Examples of PHP data access, examples of PHP data in this example for everyone to share two ways to access PHP data, we can compare, analyze the similarities and differences between the two ways, the final for the big ...

  • Related Article

    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.