Object-oriented data access

Source: Internet
Author: User

<?PHP//Data Access//Way One: already obsolete//1. Create a connection (establish a channel), need three parameters//$db =mysql_connect ("localhost", "root", "123456");//2. Select which database to manipulate//mysql_select_db ("MyDB", $db);//3. Write SQL statements//$sql = "SELECT * from Info";//4. Execute SQL statements//$result =mysql_query ($sql);//5. Fetching data from the result set//$row =mysql_fetch_row ($result);//Var_dump ($row);//While ($row =mysql_fetch_row ($result))//{//Var_dump ($row);//}//Way Two: object-oriented approach//1. Create a Connection object//$db =new mysqli ("localhost", "root", "123456", "mydb");//2. Determine if the connection is wrong//!mysqli_connect_error () or Die ("Connection Failed");/*if (Mysqli_connect_error ()) {echo "Connection failed"; Exit ();//Quit the entire program}*///3. Write SQL statements//$sql = "Selct * from Info";//4. Execute SQL statement, if execution succeeds return result set object, False if execution fails//$result = $db->query ($sql);//5. Read the data from the result set, and add a judgment before reading to determine if result is true//if ($result)//{    //Var_dump ($result->fetch_row ());//returns an array of rows of data that is an indexed array    /*while ($row =mysql_fetch_row ($result)) {var_dump ($row); }*/    //Var_dump ($result->fetch_assoc ());//returns a row of data, returning an associative array//Var_dump ($result->fetch_all ());//returns all data, and the data is a two-dimensional array//Var_dump ($result->fetch_object ());//returns a row of data, the way the data becomes an object//}//1. Making Connection Objects//$db =new mysqli ("localhost");//Find out the info table and display it in a table$db =NewMysqli ("localhost","Root","","MyDB");!mysqli_connect_error () or Die ("Connection Failed"); $sql="SELECT * from Info"; $result= $dbquery ($sql);if($result) {$a= $resultFetch_all ();    Var_dump ($a); Echo"<table width= ' 100% ' cellpadding= ' 0 ' cellspacing= ' 0 ' border= ' 1 ' >"; Echo"<tr> <td>Code</td> <td>Name</td> <td>Sex</td> <td>nation</ td> <td>Birthday</td> </tr>"; foreach($a as$v) {echo"<tr><td>{$v [0]}</td> <td>{$v [1]}</td> <td>{$v [2]}</td> <td>{$ v[3]}</td> <td>{$v [4]}</td> </tr>"; } Echo"</table>"; } $db 1=NewMysqli ("localhost","Root","","MyDB");!mysqli_connect_error () or Die ("Connection Failed"); $sql 1="SELECT * FROM Nation"; $result= $db 1->query ($sql 1); $n= $resultFetch_all (); Var_dump ($n); Echo"<select>"; foreach($n as$v) {echo"<option value= ' {$v [0]} ' >{$v [1]}</option>"; } Echo"<select>"; ?>

Object-oriented data access

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.