Jump to content


Photo

Am I getting the data from the query?


  • Please log in to reply
5 replies to this topic

#1 freshrod

freshrod
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 04 May 2006 - 01:54 PM

I posted about this before and got an answer, but while it did stop the error, it still isn't working how I wanted. Basically I have a value in a DB field that I want to check. The column is set up like this:

'alumInfo enum ('0','1') NOT NULL default='0',

I query it like this:

$sql = mysql_query
("SELECT alumInfo FROM users WHERE username='$username' AND password='$password'");

Then I want to check what the value is. If it's 0 I want to print one thing, if it's 1 I want to print something else. I've tried something like this:

$row = mysql_fetch_array($sql); // I'm thinking the problem lies here...
if($row['alumInfo'] == '0') {
echo "one thing";
} else {
echo "something else";
}

It seems to ignore the first 'echo' and only prints the last one, even though the default value is '0'.
Any suggestions? Thanks.

Nothing is foolproof to a talented fool.

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 04 May 2006 - 02:20 PM

You never query the database.
if ($result = mysql_query($sql)) {
  $row = mysql_fetch_array($result);
  if (!$row['alumInfo']) {
    echo "one thing";
  } else {
    echo "something else";
  }
}
Notice also this line... if (!$row['alumInfo']) {. 0 is false in php, so you dont need to do it the way you did. Besides, integers should not be quted as you had.

#3 freshrod

freshrod
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 04 May 2006 - 03:17 PM

Thanx for the help thorpe, but it still isn't showing the echo?

I pasted the code in that you put. I really don't understand what the problem is.
Nothing is foolproof to a talented fool.

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 04 May 2006 - 03:33 PM

Actually thorpe, it would appear like he's using strings instead of integers. So the way he had it origionally should work, and he does query the database... it's above the output.

However, if you truly intend to use integers (0,1), you should change the table and the if statement if it is currently treating those as strings.

Are you sure any of the fields actually have 0 in them??

Info: PHP Manual


#5 freshrod

freshrod
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 04 May 2006 - 03:49 PM

Thanx Ober. When I run (SELECT * FROM users;) it shows a 0 in the field. I really don't know what the deal is. I've used similar querys and such before... never had this much trouble.
Nothing is foolproof to a talented fool.

#6 freshrod

freshrod
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 05 May 2006 - 02:54 AM

Maybe I'm asking the wrong question. Is there a better way to run a query, check the data, and run an if/else of it?

Maybe my problem lies in my approach? Or the way I'm writing the query?
Nothing is foolproof to a talented fool.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users