Jump to content

[SOLVED] Question with MYSQL SELECT?


$username

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.