Results 1 to 4 of 4

Thread: Create database

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    Unanswered: Create database

    Dear all,

    I new to PHP/MySql.

    here i am trying to create a database.


    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    else
    {
    die('connected......'.mysql_error());
    }

    // create Database
    if (mysql_query("CREATE DATABASE my_db",$con))
    {
    die('Database created......'.mysql_error());
    // echo "Database created";
    }
    else
    {
    die('Error creating database: ' . mysql_error());
    // 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);

    ?>

    1. I couldn't identify that whats done. In PHPMyAdmin there is no database with name "my_db".

    2. I could see only one message on my page the "connected......" which is from line no: 9. if the CREATE DATABASE is unsucess then the other message "Error creating database:" should be displayed.

    Regards:

    Muhammad Nadeem

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    thats because you are using the die construct on both arms of the if statement.
    die halts execution of the script.

    usually dies is used to report an error message, not a success condition.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Oct 2010
    Posts
    3

    Create database

    Dear,

    Please guide me what to do?

    I tried the both die and echo but the same address.

    I am new to PHP and MySql.


    waiting for you.

    Muhammad Nadeem

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so what happened when you used to echo function?

    ironically you don't use the or die construct or examine the MySQL error / status whenyou try to create the table

    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","root","") or die('Could not connect: ' mysql_error());
    // create Database
    $qResult mysql_query("CREATE DATABASE my_db",$con)) or die('Database created...... '.mysql_error());
    // echo "Database created";

    // Create table
    mysql_select_db("my_db"$con) or die (MySQL crapped out with:".MySQLErrNo()." ".MySQLError);

    $sql = "CREATE TABLE Persons
    (
    FirstName varchar(15),
    LastName varchar(15),
    Age int
    )";

    // Execute query
    $qResult = mysql_query($sql,$con) or die (Failed to create table: ".MySQLErrNo()." ~ ".MySQLError);

    mysql_close($con);
    ?>
    I'd rather be riding on the Tiger 800 or the Norton

Tags for this Thread

Posting Permissions

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