...

View Full Version : PHP/MYSQL once logged in check id...



sir pannels
10-25-2002, 11:01 PM
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.


<?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...


#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:thumbsup:

mordred
10-26-2002, 01:09 AM
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:



$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.

sir pannels
10-26-2002, 02:07 AM
ok thanks man
i`ll go add that into the script...:)

sir pannels
10-26-2002, 02:13 AM
it worked perfect
thank you very much :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum