PHP front-end and database connections

Source: Internet
Author: User

Connect to a database (traditional method)

Mysql_connect ("Lcoalhost", "Root", "" ");   Connect the native database, connect the external machine localhost can be changed to make IP address mysql_select_db ("Txst", $db);            Select the database to operate $sql= "SELECT * from info";              Write MySQL statement $result=mysql_query ($sql);              Executes the MySQL statement var_dump (mysql_fetch_row ($result));     Each time it is executed, a row of data is returned, which can be read by loop

Connect to the database (current method)

$db =new mysqli ("localhost", "root", "" "," Test ");   Connect to the local database and assign a value to the specific library to $db!mysqli_connect_error () or Die ("Connection Failed");      Detects if the connection database is successful, fails to return "Connection failed" and exits the program if (Mysqli_connect_error ())                     //detects if the connection database is successful, returns false{    exit successfully ();              Exit program} $sql = "SELECT * from info";                     Write SQL statements $result= $db->query ($sql);                      Executes the SQL statement, returns the result set object successfully, fails back Falseif ($result)    //Determines whether the execution succeeds {    var_dump ($row = $result->fetch_row ());           Returns an array of indexes, one row for each execution, plus a loop to read more data    var_dump ($result->fetch_assoc ());              Returns an associative array that returns one row    Var_dump ($result->fetch_object ()) per execution;             Returns an object that returns     Var_dump ($result->fetch_all ()) once per execution;                Returns an indexed two-dimensional array, reading all information           }

PHP front-end and database connections

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.