PHP Data Access

Source: Internet
Author: User

Object-oriented approach//1. Create a Connection object$db=NewMysqli ("localhost", "root", "123", "MyDB");//2. Determine if the connection is wrongif(Mysqli_connect_error()){    Echo"Connection Failed"; Exit();//Exit Program}//!mysqli_connect_error () or Die ("Connection failed! //3. Write SQL statement//$sql = "SELECT count (*) from Info";//$sql = "INSERT into Info values (' p001 ', ' ', ' ', ', ') ';//4. Execute SQL statement, The query statement if execution succeeds returns the result set object, if execution fails, returns False$result = $db->query ($sql);
//!mysqli_connect_error () or Die ("Connection failed! ");//3. Write SQL statements$sql= "SELECT count (*) from Info";$sql= "INSERT into Info values (' p001 ', ' ', ' ', ', ')";//4. Execute SQL statement, query statement if execution succeeds return result set object, if execution fails return false$result=$db->query ($sql);//5. Reading data from the result setif($result){    Var_dump($result->fetch_row ());//returns an array of rows of data (indexed array)     while($row=$result-Fetch_row ()) {        Var_dump($row); }        Var_dump($result-&GT;FETCH_ASSOC ());//returns a row of data (associative array)        $shuju=$result->fetch_all ();//return all data (two-dimensional array)        Var_dump($result->fetch_object ());//returns a row of data (objects)        Echo $shuju[0] [0]; }//1. Making Connection Objects$db=NewMysqli ("localhost", "root", "123", "MyDB");//2. Determine if there is an error!Mysqli_connect_error() or die("Connection Failed! ");//3. Write SQL statements$sql= "SELECT * FROM Nation";//4. Execute SQL statements$result=$db->query ($sql);//5. Fetching Dataif($result){    $attr=$result-Fetch_all (); Echo"<select>"; foreach($attr  as $v)        {            Echo"<option value= ' {$v[0]} ' >{$v[1]} </option> "; }            Echo"</select>";}

PHP 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.