connections to databases in PHP

Source: Internet
Author: User

<?php

1. Link MySQL server

$conn = mysql_connect ("localhost", "root", 199452);

2. Select a database

$db = mysql_select_db ("Xinlangweibo");

3. Setting the Encoding

mysql_query ("Set names UTF8");

?

4. Working with the database

Increase

$sql = "INSERT into User2 (name,age) VALUES (' Xiao Ming ', 20)";

Delete

$sql = "Delete from user2 where id=4";

Modify

$sql = "Update user2 set age=30 where id=3";

$res =mysql_query ($sql, $conn);

if (! $res) {

Die ("Operation failed! ". Mysql_error ());

}

if (Mysql_affected_rows ($conn) >0) {

echo "Operation succeeded! ";

}else{

echo "Number of rows not affected";

}

5. Accept the results of the return and deal with them

The return value is bool and does not require processing

6. Close the connection and release the resource

Because $res is a Boolean value, there is no need to release resources.

Mysql_free_result ($res);

The following sentence can be written or not written

Mysql_close ($conn);

?>

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.