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
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    PHP/MYSQL once logged in check id...

    hey,
    im trying to get the id form a users table here in mysql/php...
    the script works fine. checks username and password etc
    and then use a meta refresh to send the id to the next page. tho it just seems to not exstract or print the id at all.. as you`ll see.
    Code:
    <?php
    #login.php
    #ensure both form fields have entries
    if( (!$username) || (!$password) )
    {
    header("Location:$HTTP_REFERER");
    exit();
    }
    
    #connect to MySQL
    $conn=@mysql_connect("localhost", "", "")
    or die("Could not connect");
    
    #select the specified database
    $rs = @mysql_select_db("GAME", $conn)
    or die("Could not select database");
    
    #create the query
    $sql="select * from user where username='$username' and password ='$password'" ;
    
    #execute the query
    $rs=mysql_query($sql,$conn)
    or die("Could not execute query");
    
    #get number of rows that match username and password
    $num = mysql_num_rows($rs);
    
    #if there is no match go back to log in page
    if($num==0)
    {
    header("Location:loginerror.htm");
    exit();}
    else
    {
    #otherwise user is authenticated
    $msg = "<h3>Loggin you in $username ...</h3>";
    }
    ?>
    
    <?php
    echo("<html><head><title>Log-In blah blah meta be be soon..</title><meta http-equiv='refresh' content='1; 
    
    URL=inside.php?id=$id'></head><body>");
    ?>
    
    <?php echo($msg); ?>
    
    </body></html>
    is it cuz the php tag has been closed and opened again? cuz i changed it so it was like this...
    Code:
    #otherwise user is authenticated
    $msg = "<h3>Loggin you in $username ...</h3>";
    }
    
    echo("<html><head><title>Log-In blah blah meta be be soon..</title><meta http-equiv='refresh' content='1; 
    
    URL=inside.php?id=$id'></head><body>");
    ?>
    but that just errored..
    anyway any ideas?
    thanks
    P

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's because you send only a query to the database, but don't extract the contents of the result list you get in return. Think of the return value of a succesful mysql_query() as a pointer to a big list of single records. You now have to access each single record and read the values from it's fields.

    Assuming that there can only be one possible recordset, I think this might be a quick fix:

    PHP Code:
    $rs=mysql_query($sql,$conn)
    or die(
    "Could not execute query");

    if (
    $rs && mysql_num_rows($rs) > 0) {
    $row mysql_fetch_assoc($rs);
    $id $row['id'];

    Check the manual sections about mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_row() and mysql_fetch_object() to get an impression how you can retrieve the single records from a MySQL resource.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    ok thanks man
    i`ll go add that into the script...

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    it worked perfect
    thank you very much


  •  

    Posting Permissions

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