Announcement

Collapse
No announcement yet.

What is wrong with this script?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is wrong with this script?

    Since quite a few people I know want to make login scripts for no reason, I thought I'd give coding one a shot for the heck of it. Anyway, in the script I have this code:

    PHP Code:
    //Pull data begin
    $db mysql_connect ($server$username$password) or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db($database,$db);
    $sql="SELECT user,pass FROM users WHERE uid = $uid";
    $userdata mysql_query($sql,$db);
    mysql_close($db);
    $data mysql_fetch_assoc($userdata);
    //Pull data end 
    I have recoded it several times but I keep getting this error:

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\test\login.php on line 29
    If I add this bit of code after mysql_close():

    PHP Code:
    if (!$userdata){
    print 
    'query ('.$sql.') failed';
    exit;

    I get this error:

    query (SELECT user,pass FROM users WHERE uid = 1) failed
    If I try running that query in PHPMyAdmin, it has no problem and does what I expected (have it return the data in the uid row).

    The $server, $username, $password, $database variables are correct. The problem seems to be in:

    PHP Code:
    $userdata mysql_query($sql,$db); 
    Since anything that uses that variable PHP always gives me an error on.

    Incase the MySQL close was somehow interfering with the query, I tried:

    PHP Code:
    //Pull data begin
    $db mysql_connect ($server$username$password) or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db($database,$db);
    $sql="SELECT user,pass FROM users WHERE uid = $uid";
    $userdata mysql_query($sql,$db);
    $data mysql_fetch_assoc($userdata);
    mysql_close($db);
    //Pull data end 
    Same error message. Does anyone see what is wrong with this?

  • #2
    Try mysql_fetch_array
    Computer Help Forum
    An informed rider makes their first destination the motorcycle forum at rider info.

    Comment


    • #3
      That was the first function I've tried. It didn't work. ^_^;;

      Comment


      • #4
        hi,

        if you ask me there are several coding errors in the code you provided

        PHP Code:
        //Pull data begin
        $db mysql_connect ($server$username$password) or die ('I cannot connect to the database because: ' mysql_error());
        mysql_select_db($database,$db);
        $sql="SELECT user,pass FROM users WHERE uid = $uid";
        $userdata mysql_query($sql,$db);
        mysql_close($db);
        $data mysql_fetch_assoc($userdata);
        //Pull data end 
        should be

        PHP Code:
        mysql_select_db($database);
        $sql="SELECT user,pass FROM users WHERE uid = $uid"//correct
        $userdata=mysql_query(sql); //nothing else here
        $data=mysql_fetch_assoc($userdata);
        mysql_close($db); 


        you cannot first close a database connection and then query the database ...
        Last edited by DelphiVillage; Wed 25 Feb '04, 4:21pm.

        Comment


        • #5
          $sql = "SELECT `user`,`pass` FROM `users` WHERE `uid`='$uid'";

          Ensure of course that there actually is an int $uid set.
          "63,000 bugs in the code, 63,000 bugs, you get 1 whacked with a service pack, now there's 63,005 bugs in the code."
          "Before you critisize someone, walk a mile in their shoes. That way, when you critisize them, you're a mile away and you have their shoes."
          Utopia Software - Current Software: Utopia News Pro (news management system)

          Comment


          • #6
            You dont need to use the backticks.

            Comment


            • #7
              Thanks for the help guys. I've found the problem. The $server, $username, $password, $database variables were in the if() and this code was in that ifs else. ^_^;;

              Comment

              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
              Working...
              X