Forum Moderators: coopster

Message Too Old, No Replies

Am I getting any data from the query?

Is mysql_fetch_array working?

         

freshrod

6:06 pm on May 4, 2006 (gmt 0)

10+ Year Member



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.

jatar_k

6:37 pm on May 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you used phpmyadmin or similar to check what the actual value is in the database?

you could also try echoing the value of the variable to see what is in there

$row = mysql_fetch_array($sql);
echo '<p>alumInfo is: ',$row['alumInfo'],'<p>';
if($row['alumInfo'] == '0') {
echo "one thing";
} else {
echo "something else";
}

NubKnacker2k

12:57 am on May 5, 2006 (gmt 0)

10+ Year Member



The problem is in your SQL query. It's not returning any rows so the mysql_fetch_array also returns an empty array because of which the if condition is never true.

freshrod

2:50 am on May 5, 2006 (gmt 0)

10+ Year Member



ok... so how should I write it?

I just want to check what the value is, then run an if/else based on the data.

eelixduppy

3:19 am on May 5, 2006 (gmt 0)




you could also try echoing the value of the variable to see what is in there

First see what the query is return before you do any comparison. You might not be getting anything. Just do a simple echo of $row['alumInfo'] to see what it returns, or log into your mysql and test it manually. If you find out that it is returning an empty set, then you have a problem with your query because obviously the values aren't in the table.

freshrod

5:47 am on May 5, 2006 (gmt 0)

10+ Year Member



ok.. I know there is data in the field from looking with my MySQL Command Line, but: echo '<p>alumInfo is: ',$row['alumInfo'],'<p>'; gave me no result.

So given the DB column info, any suggestion as to how I should write that query?

NubKnacker2k

10:00 am on May 5, 2006 (gmt 0)

10+ Year Member



You can see how many rows your query has returned with mysql_num_rows(). Print out that. I'm certain you'll see a 0 there.

scriptmasterdel

10:16 am on May 5, 2006 (gmt 0)

10+ Year Member



This is just a little tip really:-

Use the print_r() function to print out all the data returned from the database, this will make it easier for you to understand what is happening.

<?php
echo '<pre>';
print_r($row);
echo '</pre>';
?>

e.g.
Array
(
[alumInfo] => 0
)

Del

freshrod

1:14 pm on May 5, 2006 (gmt 0)

10+ Year Member



Thanks for all the help guys. But I still really don't know what to do.

Is there a way to do this? Is there a way to run a query, check the data returned, and run an if/else dependant on the data?

If so, how?

coopster

2:27 pm on May 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



As suggested, you should check to make sure some rows were actually returned in the result set first. You can do this by using mysql_num_rows() [php.net].

freshrod

6:25 pm on May 9, 2006 (gmt 0)

10+ Year Member



Thanks everyone. I got it figured out. I was asking the wrong question. It wasn't the code, but rather how I was refering to the variables.

jatar_k

6:26 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



good work freshrod