Php+mysql: Test connection + Basic DB operation

Source: Internet
Author: User

PHP uses MySQL, from connection, creation, to display of results

1<?PHP2     //Connect MySQL test3     $db=mysql_connect("localhost", "root", "Wenwen");4     if(!$db)5     {6            die(' Could not connect: '.Mysql_error());7     }8     Else9     {Ten         Echo"Connect success!"; One     } A  -     //Create a database -     if(!mysql_query("CREATE DATABASE my_db",$db)) the     { -         Echo"Error Creating Database:".Mysql_error(); -     } -  +     //Select Database -     mysql_select_db("my_db",$db); +  A     //Create a table at     $sql= "CREATE TABLE Persons -     ( - FirstName varchar (), - LastName varchar (), - Age int -)"; in     mysql_query($sql,$db); -  to     //Inserting Data +     mysql_query("INSERT into Persons (FirstName, LastName, age) -VALUES (' Jordan ', ' Michael ', ' 51 ')); the     mysql_query("INSERT into Persons (FirstName, LastName, age) *VALUES (' Bryant ', ' Kobe ', ' 36 ')); $ Panax Notoginseng     //Create a result set -     $sql= "SELECT * FROM Persons"; the     $result=mysql_query($sql);  +  A     //displaying results in an HTML table the     Echo"<table border= ' 1 ' > + <tr> - <th>FirstName</th> $ <th>LastName</th> $ <th>Age</th> -</tr> "; -  the      while($rs=Mysql_fetch_array($result)) -     {Wuyi         Echo"<tr>"; the         Echo"<td>".$rs[' FirstName ']. "</td>"; -         Echo"<td>".$rs[' LastName ']. "</td>"; Wu             Echo"<td>".$rs[' Age ']. "</td>"; -         Echo"</tr>"; About     } $     Echo"</table>"; -  -     //Close Connection -     Mysql_close($db); A?>

Run effect

Changes in the DB

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.