Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Creating/Populating MSQL table with PHP

    Hello,
    What is the code to populate a MSQL table with php?
    I have the code to access the database and to create the table, but how to I manually put in the data?
    PHP Code:
    <?php

    $con 
    mysql_connect("localhost","USER","PASSWORD");
    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(15),
    LastName varchar(15),
    Age int
    )"
    ;

    // Execute query
    mysql_query($sql,$con);

    mysql_close($con);

    ?>
    Last edited by rosiedee; 01-25-2012 at 05:21 PM. Reason: mistake

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Not sure I really understand your question. SQL and PHP are completely different languages, just that PHP has the ability to connect with SQL. You'll need to learn SQL in order to create the INSERT required to write table data. The command in PHP is the same: mysql_query, its the SQL command that changes.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hello, nevermind. Thank you for your response, but I found the second part of the tutorial. Once a database is created, the code to populate it would be:
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","peter","abc123");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("my_db"$con);

    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('Peter', 'Griffin', '35')"
    );

    mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
    VALUES ('Glenn', 'Quagmire', '33')"
    );

    mysql_close($con);
    ?>

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, except you shouldn't really wrap the 'age' into quotations. In strict mode this will fail since the integer datatype will not match that of the string. In loose mode, this is an implicit conversion, but will sacrifice a cycle to convert. Not a big deal with two insertions, but on say 500K insertions you will likely bring an insertion up from say 15 minutes to more like 4 hours.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •