Simple SELECT query not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Simple SELECT query not working

Post by mhouldridge »

Hi,

I cannot figure out why this query and statement does not work, please help;

Code: Select all

$user = $_SESSION['userid'];
$query = mysql_query("SELECT signoff FROM users WHERE userid = '$user'");
  
  if ($query['signoff'] == 0) {
  echo '<a href=signoff.php?var=$userid class=blue2>You have not signed the Policies and Procedures form, click here now</a>';
  }
The echo statement is printed all the time, therefore my query is not obtaining the user. Echo'ing my query does not come up within anything too.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

twigletmac | Please use

Code: Select all

tags when posting PHP code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

$query is a recource not a variable.

Code: Select all

$user = $_SESSION['userid'];
$query = mysql_query("SELECT signoff FROM users WHERE userid = '$user'");
$signoff = mysql_fetch_row[$query];
if ($signoff[0] == 0) {
  echo '<a href=signoff.php?var=$userid class=blue2>You have not signed the Policies and Procedures form, click here now</a>';
  }
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

AGISB wrote:$query is a recource not a variable.

Code: Select all

$user = $_SESSION['userid'];
$query = mysql_query("SELECT signoff FROM users WHERE userid = '$user'");
$signoff = mysql_fetch_row[$query];
if ($signoff[0] == 0) {
  echo '<a href=signoff.php?var=$userid class=blue2>You have not signed the Policies and Procedures form, click here now</a>';
  }
first you should've used PHP Tags...
second mysql_fetch_row[$query]; should be mysql_fetch_row($query);
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post by phpdevuk »

I prefer to use mysql_fetch_assoc as opposed to mysql_fetch_row, it returns an associative array of fields from the db

Code: Select all

$user = $_SESSION['userid']; 
$query = mysql_query("SELECT signoff FROM users WHERE userid = '$user'"); 
$signoff = mysql_fetch_assoc($query); 
if ($signoff['signoff'] == 0) { 

 ?><a href=signoff.php?var=<?= $userid ?> class=blue2>You have not signed the Policies and Procedures form, click here now</a><?

}
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Thankyou!
Post Reply