Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I fix this error message, 'Resource id #5'?

Here is my code:

<?

$con = mysql_connect("localhost","$user","$pass");

@mysql_select_db(a7695322_login, $con) or die("Unable to Connect");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

$query = "INSERT INTO login VALUES ('$_POST[user]','$_POST[pass])";

mysql_query($query);

if (!mysql_query ($con))

{

die ('Error: ' . mysql_error());

}

echo "Thanks for Registering";

mysql_close($con);

?>

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    First impression is that the a7695322_login should have quotes around it.

    @mysql_select_db("a7695322_login", $con) or die("Unable to Connect");

    Also, there's no reason to 'select_db' if the $con didn't work in the first place, so moving it to after the if(!$con) makes sense, or just removing the if(!$con) block altogether makes more sense.

    This would probably make even *more* sense using PDO, as it'd be safer (you're opening up yourself to SQL injection attacks with the $_POST values going directly in).

    <?php

    $dsn = "mysql:host=localhost;dbname=a7695322_login";

    $pdo = new PDO($dsn,"username","password");

    $insert = $pdo->prepare("Insert into login (user,pass) values (?,?)");

    $result = $insert->execute(array($_POST['user'], $_POST['pass']));

    if(!$result) {

    print_r($insert->errorInfo());

    die();

    }

    echo "Thanks for registering";

    unset($pdo);

    ?>

    More info can be found at http://php.net/pdo

  • Jim
    Lv 7
    1 decade ago

    have you ever thought of

    $result=mysql_query($query);

    mysql_free_result($result);

    you should always free up your result before doing another query.

    and your problem is mainly where you use your database connection for a query. mysql_query($con);

    look at the manual for mysql_query for single argument.

    ?

Still have questions? Get your answers by asking now.