Using the MySQL class for data access

Source: Internet
Author: User
Tags php and mysql

Data access is divided into three types of 1. Use a function to discard 2 in a new version. An object-oriented approach to the Mysqli class 3.PDO example <table width= "100%" border= "1" > <tr> <td> codename </td> <td> name </td> <td> gender </td> <td> National </td> <td> Birthdays </td> </tr> Use the Mysqli class to access the database: 1. The object in Mysqli is equivalent to establishing a channel between PHP and MySQL database $db = new mysqli ("localhost", "root", " "," Lian "); 2. Determine if the connection is wrong if (Mysqli_connect_error ()) {echo ' Connection failed! "; exit; If there is an error connecting, end the program}3. Write SQL statement $sql = "SELECT * from Info"; 4. Execute the prepared SQL statement $result = $db->query ($sql); If you are executing a query statement, return the result set object.   Returns TRUE or FALSE5 if a different statement is executed. Reads data from the result set object $ARR1 = $result->fetch_row (); Each time you read a bar, the returned array can use the while loop to read all the data while ($arr = $result->fetch_row ()) {var_dump ($arr);} $arr = $result->fetch_all (); Read all, return two-dimensional array var_dump ($arr); $arr = $result->fetch_assoc (); Reads a bar, returns the associative array var_dump ($arr); $arr = $result->fetch_object (); Reads a bar, returns the object Var_dump ($arr); $arr = $result->fetch_all (); foreach ($arr as $v) {//processing gender $sex = $v [2]? "    Male ":" Female "; Ethnic Processing $sql = "Select name frOm Nation where code= ' {$v [3]} ' ";    $re = $db->query ($sql); $a = $re->fetch_row (); echo "<tr> <td>{$v [0]}</td> <td>{$v [1]}</td> & lt;td>{$sex}</td> <td>{$a [0]}</td> <td>{$v [4]}</td> </tr> ";} Then the connection is PHP, as follows $db = new mysqli ("localhost", "root", "" "," Lian "), if (Mysqli_connect_error ()) {die (" Connection failed! ");} $sql = "INSERT into Xuanxiang values (0, ' 111111 ', ' a ', 1)", if ($db->query ($sql)) {echo "added successfully!)    "; Echo $db->insert_id; Take the added primary key value}else{echo "Add failed!   ";}

  

Using the MySQL class for data access

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.