PHP uses MySQL database

Source: Internet
Author: User

PHP uses the MySQL database, from the establishment connection to the display of the results.

The complete code is as follows:

<?PHP//Connect to MySQL$db=mysql_connect("localhost", "root", "root");if(!$db)  {   die(' Could not connect: '.Mysql_error()); }//Create a databaseif(!mysql_query("CREATE DATABASE my_db",$db))  {   Echo"Error Creating Database:".Mysql_error(); }//Select Databasemysql_select_db("my_db",$db);//Create a table$sql= "CREATE TABLE Persons (FirstName varchar (), LastName varchar (), age int)";mysql_query($sql,$db);//Inserting Datamysql_query("INSERT into Persons (FirstName, LastName, age) VALUES (' Peter ', ' Griffin ', ' + ')");mysql_query("INSERT into Persons (FirstName, LastName, age) VALUES (' Glenn ', ' Quagmire ', ' $ ')");//Create a result set$sql= "SELECT * FROM Persons";$result=mysql_query($sql); //displaying results in an HTML tableEcho"<table border= ' 1 ' ><tr><th>firstname</th><th>lastname</th><th>age </th></tr>"; while($rs=Mysql_fetch_array($result))  {  Echo"<tr>"; Echo"<td>".$rs[' FirstName ']. "</td>"; Echo"<td>".$rs[' LastName ']. "</td>"; Echo"<td>".$rs[' Age ']. "</td>"; Echo"</tr>"; }Echo"</table>";//Close ConnectionMysql_close($db);?>

Note: Creating a database and building a table will only be done the first time.

Effect

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.