jumpenjuhosaphat Posted November 4, 2006 Share Posted November 4, 2006 [code] else { $result = mysql_query("SELECT * FROM user"); while($row = mysql_fetch_array($result)) { if($row['id']==$_COOKIE["user"]) { $username=$row['username']; } } echo $username.' Cookie is set, logout is not set'; include("body.php"); }[/code]For some reason, the variable $username isn't being assigned any value. Is it a misuse of the $_COOKIE? Link to comment https://forums.phpfreaks.com/topic/26129-whats-wrong-with-this-code-snippet/ Share on other sites More sharing options...
fiddy Posted November 4, 2006 Share Posted November 4, 2006 Is $_COOKIE["user"] set . Is it entering the if condition... Link to comment https://forums.phpfreaks.com/topic/26129-whats-wrong-with-this-code-snippet/#findComment-119497 Share on other sites More sharing options...
jumpenjuhosaphat Posted November 4, 2006 Author Share Posted November 4, 2006 Yes, it is part of a code that first determines if the cookie is set, then if other properties are true. Here is the entire code:[code]<?$username='';//*****************************************************************************************//Open the database//*****************************************************************************************$con = mysql_connect("private","info","here");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("more_private", $con);//*****************************************************************************************//Check if a cookie is set, and if the logout submit button has been set//*****************************************************************************************if(isset($_COOKIE["user"])){ if(isset($_POST["logout"])) { setcookie("user","", time()-3600); include("refresh.php"); } else { $result = mysql_query("SELECT * FROM user"); while($row = mysql_fetch_array($result)) { if($row['id']==$_COOKIE["user"]) { $username=$row['username']; } } include("body.php"); }}//*****************************************************************************************//if the cookie isn't set, check if the signin submit button has been set//*****************************************************************************************else{ if(isset($_POST["signin"])) { $result = mysql_query("SELECT * FROM user"); while($row = mysql_fetch_array($result)) { if($row['username']==$username&&$row['password']==$password) setcookie("user", $row["id"], time()+3600, "/"); } include("refresh.php"); } else { $username='guest'; include("body.php"); }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/26129-whats-wrong-with-this-code-snippet/#findComment-119503 Share on other sites More sharing options...
toplay Posted November 4, 2006 Share Posted November 4, 2006 I believe that "user" is a MySQL reserved word. So, change the table name or use backtick marks around `user`.One should check if the SQL query worked before doing the fetch.Since you're comparing the row id to what's saved in the cookie, then why not just read that row directly from the table?Example:[code=php:0] { $result = mysql_query("SELECT `username` FROM `user` WHERE id = '{$_COOKIE['user']}'"); if (!$result) { // your error logic } else { $row = mysql_fetch_assoc($result); if ($row) { // did we get data back? $username = $row['username']; } else { $username = ''; // no data matched search criteria - handle scenario } } include("body.php"); }[/code] Link to comment https://forums.phpfreaks.com/topic/26129-whats-wrong-with-this-code-snippet/#findComment-119507 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.