Pages

Monday, January 27, 2014

How to Create a Table in PHP

One of the benefits of using the server-side language PHP is that it contains built-in tools for interacting with your MySQL databases. If you have a MySQL database set up, you can create and manage tables inside the database using PHP code alone to create connections to the database and to send it queries. Creating a simple table inside a database using PHP is a straightforward process once you know the code to do it.

Instructions

    1

    Create a MySQL database on your server. Log in to your website's control panel, search for the "Databases" or "MySQL" section, and select the "Create new database" option. You'll need to provide a database name, a username (which is sometimes the same as the database name) and a password. Keep this information handy.

    2

    Open a new PHP file. To create a table, you need to make a connection to the database and then send it a query filled with your instructions on creating the table. To make a connection, first house all the connection details inside a variable, traditionally called "$conn," and use the "mysql_select_db" command to select the specific database:
    $conn = mysql_connect("servername", "username", "password");
    mysql_select_db("databasename");
    Replace "username," "password," and "databasename" with your database information. Usually, your server name will be "localhost," but if that doesn't work you'll either need to poke around your site's control panel for the information or ask your hosting provider.

    3

    Create a variable to house your SQL query, and create the table using the CREATE TABLE query. Here is a variable containing the query to a very simple table called "books":
    $sql = "
    CREATE TABLE books (
    idnumber INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR (75),
    author VARCHAR (50),
    ); ";
    This table has only three columns: "idnumber," "name" and "author." The mess of code after "idnumber" is simply declaring that column as the table's "primary key," which ensures that each row on your table will have a unique behind-the-scenes ID number. The "name" and "author" columns are both given the field type "VARCHAR," which stands for "various characters" and is the basic type for a simple text field. The number afterward is the maximum amount of characters each field can be. Keep these numbers as small as you think you can get away with to save space.

    4

    Add a row to the table using the INSERT query. Since it's a separate query, you'll have to create a new variable for it:
    $sql2 = "
    INSERT INTO books VALUES
    (null, 'Oliver Twist', 'Charles Dickens');
    ";
    The "null" simply lets the MySQL database know that that's where the primary key (the unique ID number) should go. MySQL will assign the keys automatically. The next two fields fill in the "name" and "author" columns, respectively.

    5

    Put everything together with the mysql_query command. You'll have to run it twice to execute both queries:
    mysql_query($sql, $conn);
    mysql_query($sql2, $conn);

    6

    Save your PHP file (and make sure you house everything in the file inside the "" PHP tags. When run, it should create the "books" table in your database. Put all together, the code should look like this:
    $conn = mysql_connect("localhost," "demandstudios," "demandstudios") or die(mysql_error());
    mysql_select_db("test");
    $sql = "
    CREATE TABLE books (
    idnumber INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR (75),
    author VARCHAR (50)
    ); ";
    $sql2 = "
    INSERT INTO books VALUES
    (null, 'Oliver Twist', 'Charles Dickens');
    ";
    mysql_query($sql, $conn) or die(mysql_error());
    mysql_query($sql2, $conn) or die(mysql_error());
    ?>

0 comments:

Post a Comment