PHP MySQL Base operation

Source: Internet
Author: User

MySQL connection operation

Establish a connection
$con = mysql_connect (' localhost ', ' root ', ' 123456 ');
Determine if the connection is successful
if ($con) {
Die (' Connection failed! '). Mysql_error ());
Exit
}

Select a character Set
Mysql_set_charset (' UTF8 ', $con);
Select Database
mysql_select_db (' Test ', $con);

$sql = ' .......... '; '; '; ';

Execute SQL statement
$result = mysql_query ($sql, $con);

If the insert operation
Mysql_affected_rows () Gets the number of rows affected
MYSQL_INSERT_ID () Gets the newly inserted ID

If the query operation
while ($row = Mysql_fetch_assoc ($result)) {
Echo ' <pre> ';
Var_dump ($row);
Echo ' </pre> ';
}

Close the database
Mysql_close ($con);

1 //Database Connection2   functionMysqlconnect () {3     $con=mysql_connect("localhost", "root", "" ");4     if(!$con){5          die("Connection Failed!".Mysql_error());6         Exit;7     }8     9Mysql_set_charset ("UTF8",$con);Ten     mysql_select_db("MS",$con); One      A     /* - $sql = "SELECT * from User;"; - $result = mysql_query ($sql); the      - if (mysql_num_rows ($result)) { - While ($rows = Mysql_fetch_assoc ($result)) { - echo ' <pre> '; + Var_dump ($rows); - echo ' </pre> '; +         } A     } at     */ -      -     $sql= "INSERT into user (username) values (' test123 ')"; -     $result=mysql_query($sql); -      -     if($result&&mysql_affected_rows() >0){ in         Echo"Insert succeeded!".mysql_insert_id(); -     } to      +     Mysql_close($con); -}

PHP MySQL Base operation

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.