PHP Note Six: Working with MySQL tables (creating tables)

Source: Internet
Author: User

1. SQL statements Used

MySQL -u root-p Enter password to   connect to MySQL Database
SHOW DATABASES; View all databases in MySQL
Create DATABASES for creating a database
Drop database for deleting databases
Use database name Select database
SHOW tablses; To view all tables in a database
CREATE Table table name (field name type length constraint null); Create a Table statement
DESC table name view table structure

2. Example:

<?PHP//send a SQL statement that creates a data table to the MySQL database     $link= @mysql_connect(' localhost ', ' root ', ' root '); //determine if the connection database is successful//Note add an exclamation mark, otherwise the connection database fails with the error number 0, cause 0     if(!$link){           die("Connection database failed with error number:".)Mysql_errno() . "Reason for failure".Mysql_errno()); }     //Select Database     $db=mysql_select_db("Test"); //determine if selecting a database is successful     if(!$db){          die("Select database failed with error number:".)Mysql_errno() . "Reason for failure".Mysql_errno()); }     //Assemble SQL statements that create tables     $sql= "CREATE TABLE students ("; $sql. = "Stuid int (4) NOT NULL Auto_increment primary key,"; $sql. = "Stuname varchar () NOT NULL,"; $sql. = "Stusex tinyint not NULL default 1,"; $sql. = "Stubirth date not NULL,"; $sql. = "ClassId int (4) not null);"; //Execute CREATE TABLE statement     if(mysql_query($sql)){         Echo"CREATE TABLE succeeded"; }Else{         Echo"Failed to create TABLE"; }     //Close the database connection when the operation is complete     Mysql_close($link); 

PHP Note Six: Working with MySQL tables (creating tables)

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.