Jump to content

Archived

This topic is now archived and is closed to further replies.

freshrod

Am I getting the data from the query?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
You never query the database.
[code]
if ($result = mysql_query($sql)) {
  $row = mysql_fetch_array($result);
  if (!$row['alumInfo']) {
    echo "one thing";
  } else {
    echo "something else";
  }
}
[/code]
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

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.