PHP Database Programming---mysqli extension library

Source: Internet
Author: User
Tags php database

The 1,mysqli extension Library allows us to access the MySQL database and perform curd operations on the MySQL database. mysqli extension cubby MySQL extension library has been enhanced.

Comparison of 2,mysqli extension libraries and MySQL extension libraries

The stability and safety of ①mysqli, the efficiency is improved;

②mysqli supports object-oriented programming, while the mysqli extension library takes into account the old PHP programmer, providing a process-oriented programming style.

3,mysqli has two sets of programming styles

4,mysqli Programming QuickStart, writing a program that reads data from the User1 data sheet and prints it in a Web page.

① Object-oriented implementation

<?php//mysqli operation MySQL Database (object oriented style)//1, create mysqli object $mysqli = new Mysqli ("127.0.0.1", "root", "123456", "test");// Verify if Okif ($mysqli->connect_error) {die    ("Connection failed! ". $mysqli->connect_error);} 2, Operation database (send sql) $sql = "SELECT * from User1";//$res is the result set. mysqli result$res = $mysqli->query ($sql);//3, processing result while ($row = $res->fetch_row ()) {    foreach ($row as $key = + $val) {        echo "--$val";    }    echo "<br/>";} 4, close the resource//release resource $res->free ();//close connection $mysqli->close ();

② Process-oriented implementation

<?php//mysqli operation MySQL database (for process style)//1, get mysqli connection $mysqli = Mysqli_connect ("127.0.0.1", "root", "123456", "test"); if ( ! $mysqli) {die    ("Connection Failed". Mysqli_connect_error ($mysqli));} 2, send SQL statements to the database (Ddl,dml dql ..... ) $sql = "SELECT * from User1"; Mysqli_query ($mysqli, $sql);//3, processed results//loop out $res data Mysqli_fetch_row Mysql_fetch_ Rowwhile ($row =mysqli_fetch_row ($res)) {    foreach ($row as $key + $val) {        echo "--$val";    }    echo "<br/>";} 4, close the resource//release resource Mysqli_free_result ($res);//close connection mysqli_close ();

5, the $res in the program is used to represent the execution result of the SQL statement

① If a DML statement is executed, BOOL is returned;

② If the DQL statement is executed, the query result mysqli_result result set object is returned;

6, the result of fetching the query from MYSQLI result has the following way:

①mysqli_result::fetch_assoc

②mysqli_result::fetch_row

③mysqli_result::fetch_array

④mysqli_result::fetch_object

PHP Database Programming---mysqli extension library

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.