Results 1 to 5 of 5

Thread: How to create MySQL Database and Tables in PHP?

  1. #1
    Join Date
    Apr 2009
    Posts
    745

    How to create MySQL Database and Tables in PHP?

    Hi friends,
    I have done the programming in PHP to some extent. Now I am working with the database. I am using MySQL for database. I am not having fair knowledge about the using the MySQL in PHP. So I need your help.!! Please tell me how to create MySQL Database and Tables in PHP? Any other information regarding the topic would be grateful.

  2. #2
    Join Date
    Jul 2006
    Posts
    442

    Re: How to create MySQL Database and Tables in PHP?

    A database will contain a variable number of tables of data which are arranged in columns and rows. A MySQL table is completely different than the normal coding that you do. You might feel tough to understand but later on you will do it easily. In MySQL and other database systems, the goal is to store information in an orderly fashion. You can do this by using the tables. Also I would like to suggest you that before you can enter data (rows) into a table, you must first define what kinds of data will be stored (columns).
    "When they give you ruled paper, write the other way..." J.R.J.

  3. #3
    Join Date
    Mar 2008
    Posts
    349

    Re: How to create MySQL Database and Tables in PHP?

    You can use the following code for creating the table in PHP :
    PHP Code:
    <?php
    mysql_connect
    ("localhost""admin""1admin") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());

    mysql_query("CREATE TABLE example(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
     name VARCHAR(30), 
     age INT)"
    )
     or die(
    mysql_error());  

    echo 
    "Table Created!";

    ?>
    the above code will design a MySQL query to summon the table from database.

  4. #4
    Join Date
    Nov 2008
    Posts
    996

    Re: How to create MySQL Database and Tables in PHP?

    Creating the database is also very important. The following example creates a database called my_data :
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","sunil","xyz123");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    if (
    mysql_query("CREATE DATABASE my_data",$con))
      {
      echo 
    "Database created";
      }
    else
      {
      echo 
    "Error creating database: " mysql_error();
      }

    mysql_close($con);
    ?>

  5. #5
    Join Date
    Aug 2006
    Posts
    227

    Re: How to create MySQL Database and Tables in PHP?

    You should also know about the primary keys and auto increment fields. You should keep in mind that each table should have a primary key field. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field. The following code would help you :
    Code:
    $sql = "CREATE TABLE PersonsDemo 
    (
    personID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(personID),
    FirstName varchar(15),
    LastName varchar(15),
    Age int
    )";
    
    mysql_query($sql,$con);
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

Similar Threads

  1. How to detect failure of MySQL database tables
    By Agneya in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 04:40 AM
  2. How to use Batch Files to Create MySQL Database?
    By DANIEL 602 in forum Software Development
    Replies: 4
    Last Post: 05-02-2010, 07:03 AM
  3. how to create and restore mysql database using vb 6.0
    By nanakofiboafo in forum Software Development
    Replies: 1
    Last Post: 14-09-2009, 09:18 AM
  4. Sample database script to create tables
    By Naresh Modi in forum Software Development
    Replies: 3
    Last Post: 12-08-2009, 02:17 PM
  5. Mysql select from multiple tables for php.
    By afidelino in forum Software Development
    Replies: 4
    Last Post: 09-07-2009, 06:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,721,086,984.17585 seconds with 17 queries