www.webdeveloper.com
Results 1 to 5 of 5

Thread: database connectivity problem

  1. #1
    Join Date
    Nov 2012
    Posts
    2

    database connectivity problem

    I used the following code for inserting form data into "userinfo" table of "user" database.The database and table was created on xampp(phpmyadmin).But the page displayed "Error: No database selected".... why?pls help..


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

    mysql_select_db("user",$con);

    $sql="INSERT INTO userinfo(id,pass)
    VALUES
    ('$_POST[UID]','$_POST[pass]')";

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

    mysql_close($con);
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    Put a test on the result of mysql_select_db() and make sure it's working.

    (PS: Modern PHP apps should really be using either the MySQLi or PDO extension to interface with MySQL.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2012
    Posts
    2
    ok..i put the check on "mysql_select_db" and its not working...what can i do for it now??

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    What is the error? (You can use mysql_error() there just like for any other db error.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Nov 2012
    Posts
    3
    Okay, try something like this:
    CODE:
    <?php
    $con = mysql_connect("localhost","","");
    mysql_select_db("user",$con);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    $sql="INSERT INTO userinfo(id,pass)
    VALUES
    ('$_POST[UID]','$_POST[pass]')";
    $query = mysql_query($sql,$con)
    if (!$query)
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

    mysql_close($con);
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles