PHP Operation MySQL

Source: Internet
Author: User

Functions in PHP that manipulate MySQL database

PHP initially provides a set of connection operations MySQL function mysql_xxx ();
Later, an enhanced version of the function mysqli_xxx () was provided;

Connect MySQL server steps with PHP
1: Create a connection to the MySQL server
$conn = Mysqli_connect (url,uname,upwd,dbname);
URL MySQL server address ' 127.0.0.1 '
uname MySQL user name ' root '
upwd mysql password '
dbname Connect which library ' Dangdang '
2: Send SQL statement to MySQL server
$result = mysqli_query (data connection, SQL statement);
3: Read MySQL server return results
if ($result ===true) {
Successful execution
}else{
Execution failed
}
4: Disconnect from MySQL server---(can be omitted)
Mysqli_close ($conn);

Example: 8.php Add data
Dangdang/dd_category (Id,name,bcount);
Pit:
php-> database record garbled!
Cause: Php-->mysql Add data by default
Code latin_1
Workaround: Add a line after creating the connection
$conn = Mysqli_conn ....
Mysqli_query ($conn, "SET NAMES UTF8");
PHP sends SQL transcoding--LATIN_1-->UTF8

Instance code:

<?PHP$conn=Mysqli_connect(' 127.0.0.1 ', ' root ', ', ' Dangdang ') or die(' Server Connection failed! ‘); Mysqli_query($conn, ' Set names UTF8 '); $sql= ' SELECT * from Dd_book '; $re=Mysqli_query($conn,$sql); if($re){        foreach($re  as $k=$v){            Var_dump($k,$v); }    }Else{        Throw New Exception(' Data table query failed! ‘); }

PHP Operation MySQL

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.