PHP Basics (12) using PHP to operate MySQL

Source: Internet
Author: User
Tags deprecated php basics

PHP operation MySQL A total of five steps

    1. Connect to MySQL

    2. Select Database

    3. Execute SQL statement

    4. Close result set

    5. Disconnecting from the MySQL server


=============================================================

PHP uses three kinds of API operation MySQL is MySQL (new PHP deprecated) mysqli pdoextensions

<?php// mysqli$mysqli = new mysqli ("example.com",  "user",  "password",  " Database "), $result  =  $mysqli->query (" select  ' hello, dear mysql user! '  as _message from dual "); $row  =  $result->fetch_assoc (); echo htmlentities ($row [' _message ']);// pdo$pdo = new pdo (' Mysql:host=example.com;dbname=database ',  ' User ',  ' password '); $statement  =  $pdo->query ("select " hello, dear mysql  user! '  as _message from dual "); $row  =  $statement->fetch (PDO::FETCH_ASSOC); echo  htmlentities ($row [' _message ']);// mysql$c = mysql_connect ("example.com",  "user",   "password"); mysql_select_db ("database"); $result  = mysql_query ("select " hello, dear  mysql user! '  as _message from dual "); $row  = mysql_fetch_assoc ($result);echo  Htmlentities ($row[' _message ');? >


The last method is deprecated, and has been discarded in PHP5.5.0. The recommended way to use MYSQLI,PDO extensions.


Which library do you choose? All three of these APIs are lightweight packages for C libraries.

There are two options mysqlnd,libmysqlclient recommend using MYSQLND, which is built in PHP from PHP5.3.0 and can provide features such as lazy connections and query caching.


There are several concepts that need to be understood when working with MySQL using the PHP API

    1. Buffered and unbuffered queries

PHP query By default using buffer mode, that is, the query results are returned to PHP by the MySQL server in the memory of the PHP process, so that the query results can be further operations, the disadvantage is that it may be too large memory consumption. (also known as store result)

PHP's non-buffered mode returns a resource after the query, but the query result data is still on the MySQL server. This allows PHP to consume less memory, but the load on the MySQL server will increase. (also known as use result)

Therefore, the buffering mode is suitable for cases where the query results are small, and the non-buffering mode is suitable for the large number of query results.

An example of using mysqli non-buffered queries

<?php$mysqli = new Mysqli ("localhost", "My_user", "My_password", "World"); $uresult = $mysqli->query ("Select Name F ROM City ", Mysqli_use_result), if ($uresult) {while ($row = $uresult->fetch_assoc ()) {echo $row [' Name '].   Php_eol; }} $uresult->close ();? >

Examples of non-buffered queries using Pdo_mysql

<?php$pdo = new PDO ("Mysql:host=localhost;dbname=world", ' my_user ', ' my_pass '); $pdo->setattribute (PDO::MYSQL_ Attr_use_buffered_query, false); $uresult = $pdo->query ("Select Name from City"), if ($uresult) {while ($row = $uresul T->fetch (PDO::FETCH_ASSOC)) {echo $row [' Name '].   Php_eol; }}?>

=================== How to configure mysql============================== using Phpstorm

View--->tool Windows--->database

Enter host name Username,password

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/87/F5/wKioL1fk9m7yEMBpAACcxKolE58944.jpg-wh_500x0-wm_3 -wmp_4-s_259015832.jpg "title=" 1.jpg "alt=" Wkiol1fk9m7yembpaaccxkole58944.jpg-wh_50 "/>


650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/87/F5/wKioL1fk9p3xN7gfAABenlXbgfw723.jpg-wh_500x0-wm_3 -wmp_4-s_2761862847.jpg "title=" 2.jpg "alt=" Wkiol1fk9p3xn7gfaabenlxbgfw723.jpg-wh_50 "/>

Click MySQL to install the driver

This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1855911

PHP Basics (12) using PHP to operate MySQL

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.