forumnz Posted May 21, 2009 Share Posted May 21, 2009 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 More sharing options...
co.ador Posted May 21, 2009 Share Posted May 21, 2009 <?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 Link to comment https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/#findComment-838676 Share on other sites More sharing options...
mmarif4u Posted May 21, 2009 Share Posted May 21, 2009 Still not sure about your question. If num is not zero so ofcourse it will print Test not Hey as your if statement stated that. Correct me, if i m wrong. Link to comment https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/#findComment-838677 Share on other sites More sharing options...
BobcatM Posted May 21, 2009 Share Posted May 21, 2009 What co.ador said is the only thing i can find, and mmarif4u is also correct. So it should echo "Test!" Link to comment https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/#findComment-838679 Share on other sites More sharing options...
co.ador Posted May 21, 2009 Share Posted May 21, 2009 did your problem got solved? Link to comment https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/#findComment-838681 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 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. Link to comment https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/#findComment-838682 Share on other sites More sharing options...
BobcatM Posted May 21, 2009 Share Posted May 21, 2009 That's my your the pro and I am a newb. Thanks for the info Ken. Link to comment https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/#findComment-838689 Share on other sites More sharing options...
co.ador Posted May 21, 2009 Share Posted May 21, 2009 die("Database query failed:" . mysql_error()); Ken2k7 was right I didn't pay full attention at the end of that line but instead of mysql_error to be inside the parentheses then you should state it as above. hope that solve your problem. Link to comment https://forums.phpfreaks.com/topic/159031-solved-why-wont-this-query-work/#findComment-838700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.