$username Posted September 17, 2007 Share Posted September 17, 2007 In my code I am trying to SELECT username and UsrRights When I Echo out the SQL query I do not get anything back. Why am I not getting anything echoed back? include 'dbopen.php'; include 'dbconnect.php'; $username = $_COOKIE["user"]; $sql = mysql_query ("SELECT UsrRights FROM 'admin' WHERE username = '$username' "); $sql2 = mysql_query ("SELECT username FROM 'admin' WHERE username = '$username' "); $usrrights = $sql; $username2 = $sql2; ?> <div id="nav-menu"> <ul> <li><a href="show.php">Show Cases</a></li> <li><a href="write.php">Add New Case</a></li> <li><a href="showusers.php">Show Clients</a></li> <li><a href="logoff.php">Log Off</a></li> <li><?php if ($username == $username2 && $usrrights == 120) echo "<a href=\"logoff.php\">Log Off</a>"; ?> </li> </ul> </div> <?PHP echo "$username Username"; echo "$username2 username2 "; echo "$usrrights userrights"; ?> Thank you, Brett Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/ Share on other sites More sharing options...
rarebit Posted September 17, 2007 Share Posted September 17, 2007 No need for quotes around the table name, also try using or die for errors! $sql = mysql_query ("SELECT UsrRights FROM admin WHERE username = '$username' ") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349964 Share on other sites More sharing options...
$username Posted September 17, 2007 Author Share Posted September 17, 2007 test UsernameResource id #7 username2 Resource id #8 userrights This is what I get when I do not use '' around the table name And this is what I get for the MYSQL error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''admin' WHERE username = 'test'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349966 Share on other sites More sharing options...
BlueSkyIS Posted September 17, 2007 Share Posted September 17, 2007 it looks like you expect the result of mysql_query() to be data. It's not. $usrrights = $sql; // This will never be 120. try something like this: $result1 = mysql_query ("SELECT UsrRights FROM 'admin' WHERE username = '$username' ") or die(mysql_error()); $result2 = mysql_query ("SELECT username FROM 'admin' WHERE username = '$username' ") or die(mysql_error()); list($usrrights) = mysql_fetch_row($result1); list($username2) = mysql_fetch_row($result2); Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349968 Share on other sites More sharing options...
$username Posted September 17, 2007 Author Share Posted September 17, 2007 Here is the value in the database. password username ID secvalue UsrRights 5F4DCC3B5AA765D61D8327DEB882CF99 test 1 1 120 Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349970 Share on other sites More sharing options...
stervyatnik Posted September 17, 2007 Share Posted September 17, 2007 Not sure I can help, but here goes! When you echo something, you have to set variables apart with periods and quote in the syntax, as this: $myname = 'Dan'; echo "<p>My name is ".$myname." and that's all there is to it!</p>"; The output will be: My name is Dan and that's all there is to it! Your code had - if I remember correctly - an echo like this: //you generated an SQL query and loaded your result into variables like $username1 and $username2 echo "$username1 $username2 etc..."; You should try this with your variables: //you generated an SQL query and loaded your result into variables like $username1 and $username2 //for example $username1 = 'Joe' and $username2 = 'Fred' echo $myname1." ".$myname2." etc..."; Your output will be: Joe Fred etc... Hope that helps! Y Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349972 Share on other sites More sharing options...
BlueSkyIS Posted September 17, 2007 Share Posted September 17, 2007 "When you echo something, you have to set variables apart with periods and quote in the syntax, as this;" No, that is wrong. echo "<p>My name is $myname and that's all there is to it!</p>"; is exactly the same as echo "<p>My name is ".$myname." and that's all there is to it!</p>"; THE PROBLEM is that we are calling mysql_query(), which returns a mysql resource id, NOT a row of data. To get the data, we still have to call mysql_fetch_row() or similar, using the returned resource id. Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349975 Share on other sites More sharing options...
stervyatnik Posted September 17, 2007 Share Posted September 17, 2007 To clarify, using your code: <? include 'dbopen.php'; include 'dbconnect.php'; $username = $_COOKIE["user"]; $sql = mysql_query ("SELECT UsrRights FROM 'admin' WHERE username = '$username' "); $sql2 = mysql_query ("SELECT username FROM 'admin' WHERE username = '$username' "); $usrrights = $sql; $username2 = $sql2; ?> <div id="nav-menu"> <ul> <li><a href="show.php">Show Cases</a></li> <li><a href="write.php">Add New Case</a></li> <li><a href="showusers.php">Show Clients</a></li> <li><a href="logoff.php">Log Off</a></li> <li><?php if ($username == $username2 && $usrrights == 120) echo "<a href=\"logoff.php\">Log Off</a>"; ?> </li> </ul> </div> <?PHP echo "$username Username"; echo "$username2 username2 "; echo "$usrrights userrights"; ?> Your last three lines are the problem. They should be: echo $username." Username"; echo $username2." username2"; echo $usrrights." " userrights"; Hope that helps, too! Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349976 Share on other sites More sharing options...
BlueSkyIS Posted September 17, 2007 Share Posted September 17, 2007 ^^^ WRONG! $sql = mysql_query ("SELECT UsrRights FROM 'admin' WHERE username = '$username' "); $sql2 = mysql_query ("SELECT username FROM 'admin' WHERE username = '$username' "); list($usrrights) = mysql_fetch_row($sql); list($username2) = mysql_fetch_row($sql2); Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-349977 Share on other sites More sharing options...
$username Posted September 17, 2007 Author Share Posted September 17, 2007 This is how I fixed it. <?php if (isset($_COOKIE["user"])) setcookie("user", ($_COOKIE["user"]), time()+600); else header("Location:cookerr.htm"); //echo "Welcome guest!<br />"; ?> <?PHP include 'dbopen.php'; include 'dbconnect.php'; $username = $_COOKIE["user"]; $result1 = "SELECT UsrRights FROM admin WHERE username = '$username' "; $result2 = "SELECT username FROM admin WHERE username = '$username' "; $query1 = mysql_query($result1); $query2 = mysql_query($result2); list($usrrights) = mysql_fetch_row($query1); list($username2) = mysql_fetch_row($query2); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <link href="./css/master.css" rel="stylesheet" type="text/css" /> </script> <html> <div id="nav-menu"> <ul> <li><a href="show.php">Show Cases</a></li> <li><a href="write.php">Add New Case</a></li> <li><a href="showusers.php">Show Clients</a></li> <li><a href="logoff.php">Log Off</a></li> </ul> </div> <?PHP echo "$username Username"; echo "$usrrights username2 "; echo "$username2 userrights"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/69652-solved-question-with-mysql-select/#findComment-350007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.