PHP MySQL CREATE DATABASE and presentation example

Source: Internet
Author: User


    1. <?php

    2. Create a database

    3. $con = mysql_connect ("localhost", "Peter", "abc123");

    4. if (! $con)

    5. {

    6. Die (' Could not connect: '. Mysql_error ());

    7. }

  • if (mysql_query ("CREATE DATABASE my_db", $con))

  • {bbs.it-home.org

  • echo "Database created";

  • }

  • Else

  • {

  • echo "Error Creating Database:". Mysql_error ();

  • }

  • Mysql_close ($con);

  • ?>

  • 2, CREATE table to create tables for database tables in MySQL.

    Syntax Create TABLE table_name (column_name1 data_type,column_name2 data_type,column_name3 data_type,.......) In order to execute this command, I must add the CREATE TABLE statement to the mysql_query () function.

    example, create a table named "Persons", which has three columns. The column names are "FirstName", "LastName", and "age":




      1. <?php

      2. $con = mysql_connect ("localhost", "Peter", "abc123");

      3. if (! $con)

      4. {

      5. Die (' Could not connect: '. Mysql_error ());

      6. }

  • Create Database

  • if (mysql_query ("CREATE DATABASE my_db", $con))

  • {bbs.it-home.org

  • echo "Database created";

  • }

  • Else

  • {

  • echo "Error Creating Database:". Mysql_error ();

  • }

  • Create table in my_db database

  • mysql_select_db ("my_db", $con);

  • $sql = "CREATE TABLE Persons

  • (

  • FirstName varchar (15),

  • LastName varchar (15),

  • Age int

  • )";

  • mysql_query ($sql, $con);

  • Mysql_close ($con);

  • ?>


  • Copy Code

    Important: Before you create a table, you must first select the database. Select the database by using the mysql_select_db () function.

    Note: When you create a database field with a varchar type, you must specify the maximum length of the field, for example: varchar (15).

    MySQL data type various MySQL data types:

      1. <?php

      2. $sql = "CREATE TABLE Persons

      3. (

      4. PersonID int not NULL auto_increment,

      5. PRIMARY KEY (PersonID),

      6. FirstName varchar (15),

      7. LastName varchar (15),

      8. Age int

      9. )";

  • 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.