PHP's MySQL creation database and table

Source: Internet
Author: User

PHP's MySQL creation database and table

The database has one or more tables.

Creating a Database
The Create DB statement is used to build a database in MySQL.

Grammar

CREATE DATABASE database_name
   
   

To learn more about SQL, please visit our SQL Tutorial.
In order for PHP to execute the above statement, we must use the mysql_query () function. This feature is used to send a MySQL connection to a query or command.

For example
The following example creates a database called "my_db":

<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
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_close ($con);
? >
     
     

Create a table
The CREATE table declaration is used to create a table in MySQL.

Grammar

CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type, 
Column_name3 data_type,
...
)
       
       

To learn more about SQL, please visit our SQL Tutorial.

We must add the CREATE table declaration, mysql_query () function to execute the command.

For example
The following example creates a table named "People," with three columns. The column name will be "first name", "surname" and "age":

<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
  {
  die (' Could not connect: '. Mysql_error ());
  }
Create Database
if (mysql_query ("CREATE Database my_db, $con)"
  {
  echo "database created";
  }
else
  {
  echo "Error Creating Database:". Mysql_error ();
  }
Create table
mysql_select_db ("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar (),
LastName varchar (), age
int
)";
Execute Query
mysql_query ($sql, $con);
Mysql_close ($con);
? >
 
          
          


















For example

$sql = "CREATE TABLE Persons
(
personID int not NULL auto_increment, 
PRIMARY KEY (PersonID),
FirstName VA Rchar (),
LastName varchar (), age
int
) ";
mysql_query ($sql, $con);

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.