(3.24) How PHP connects data

Source: Internet
Author: User

There are two main ways in which PHP connects to MySQL database:

1. Process-oriented approach: not recommended

// 1. Obsolete methods    /* //Generate connection    $db _connect=mysql_connect ("localhost", "root", "324528");        Select the Operational database    mysql_select_db ("Testa", "$db _connect");         Write SQL statement    $sql = "SELECT * from Info";         Execute SQL statement    $result =mysql_query ($sql);        Processing Queries    $row =mysql_fetch_row ($result);         Print_r ($row); */

2. The object-oriented approach is the most common way to connect:

This method of PHP connection to the database is mainly divided into: (1) Create a connection (2) Determine whether the connection succeeds (3) write the SQL statement (4) Execute the SQL statement (5) to process the result

       1. Create the connection $db= new mysqli ("localhost", "root", "324528", "Testa"),//2. Determine if the connection was successful!mysqli_connect_error () or Die ("Connection Failed");// 3. Write SQL statements $sql= "SELECT * from Info";//4. Execute SQL statement $result= $db->query ($sql);//5. Operation result Data//(1) Fetch_row ()//$row =$ Result->fetch_row ();/*while ($row = $result->fetch_row ()) {Print_r ($row);} *///(2) Fetch_all () returns all data in the form of a two-dimensional array/* $attr = $result->fetch_all ();p rint_r ($attr); *///(3) Fetch_assoc (); Returns a piece of data, in the form of an associative array,/* $attr = $result->fetch_assoc ();p rint_r ($attr); */

(3.24) How PHP connects data

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.