Jump to content

[SOLVED] Why won't this query work?


forumnz

Recommended Posts

I'm not a total newb in PHP and I do the following all the time, but for one reason or another this code snippet won't work.. why not?

 

<?php
$sqlc = mysql_query("SELECT * FROM current_org WHERE user_hash='$uh' AND hash_key='$hk' ORDER BY date DESC LIMIT 1") or die(mysql_error);
$num = mysql_num_rows($sqlc);
echo $num;
while($rowc = mysql_fetch_array($sqlc))
{

if($num == '0')
{
echo "Hey!";
}
else{
echo "Test!";
}
}
?>

 

All it does is display something like:

1Test!

1Test!

0000000000000

 

Meaning that there are two rows existing.. but where is the echoed "Hey!"?

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/
Share on other sites

<?php
$sqlc = mysql_query("SELECT * FROM current_org WHERE user_hash='$uh' AND hash_key='$hk' ORDER BY date DESC LIMIT 1") or die(mysql_error);
$num = mysql_num_rows($sqlc);
echo $num;
while($rowc = mysql_fetch_array($sqlc))
{
   
if($num == '0')
{
echo "Hey!";
}
else{
echo "Test!";
}
}
?>

 

you should be exhausted because there is an obvious error

 

you are using the line below:

while($rowc = mysql_fetch_array($sqlc))

 

you should put $num instead of $sqlc

while($rowc = mysql_fetch_array($num))

 

try it i might be wrong

 

 

1. co.ador is wrong. mysql_fetch_array($num) is an error because $num is not a MySQL resource.

2. Placing an if ($num == 0) inside the while loop is stupid because if you're inside the while loop, you know $num isn't 0.

3. Your die statement has an undefined constant. It should be mysql_error() not mysql_error.

Archived

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

×
×
  • 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.