PHP MySQL Database

Source: Internet
Author: User

1. Connect to the database

Mysql-u root-p

2. Show several databases

Show Databses

3. Create a Database

<?php$con = mysql_connect ("localhost", "root", "a76d290e04"), if (! $con) {die (' Could not connect: '. Mysql_error () );} if (! $con) {die (' Could not connect: '. Mysql_error ());} if (mysql_query ("CREATE DATABASE my_db", $con)) {echo "DATABASE created";} else {echo "Error creating DATABASE:". Mysql_error ();} mysql_select_db ("my_db", $con); $sql = "CREATE TABLE Persons (FirstName varchar (), LastName varchar (), age int)"; MySQL _query ($sql, $con); Mysql_close ($con);  ? >

Two. Inserting data

<body><?php$con = mysql_connect ("localhost", "root", "a76d290e04"), if (! $con) {die (' Could not connect: '). Mysql_error ());} if (! $con) {die (' Could not connect: '. Mysql_error ());} mysql_select_db ("my_db", $con); $sql = "INSERT into Persons (FirstName, LastName, age) VALUES (' $_post[firstname] ', ' $_po St[lastname] ', ' $_post[age] '), if (! mysql_query ($sql, $con)) {die (' Error: '. mysql_error ());} echo "1 record added"; Mysql_close ($con);? ><form action= "index.php" method= "POST" >firstname: <input type= "text" name= "Firstname"/> Lastname: <input type= "text" name= "LastName"/>age: <input type= "text" name= "age"/> <input type= "Submit"/>< /form></body>


Three. Querying data

<?php$con = mysql_connect ("localhost", "root", "a76d290e04"), if (! $con) {die (' Could not connect: '. Mysql_error () );} if (! $con) {die (' Could not connect: '. Mysql_error ());} mysql_select_db ("my_db", $con); $result = mysql_query ("SELECT * from Persons") and while ($row = Mysql_fetch_array ($resul T) {echo $row [' FirstName ']. " " . $row [' LastName '];echo "<br/>";} Mysql_close ($con);? >



PHP MySQL Database

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.