MDanz Posted August 19, 2010 Share Posted August 19, 2010 this foreach loop is causing mysql_num_rows to not return the amount of rows in mysql correctly. the variable $value has two strings in it (testa and testb). So foreach should loop once with $value as 'testa', then loop again with $value as 'testb'. the problem is at the mysql_num_rows. It should return 1 because there is 1 row in mysql with keywords 'testa' and 1 row with keywords 'testb', instead it returns zero. If i change $value to 'testa' it works. foreach($value as $value) { $updatestable = mysql_query("SELECT * FROM `Stacks` WHERE keywords LIKE '%$value%' ORDER BY id DESC LIMIT 1")or die (mysql_error()); $upnum = mysql_num_rows($updatestable); echo $upnum; if ($upnum==0){ $uporigin=0; } else { while($rowup = mysql_fetch_assoc($updatestable)) { $uporigin = $rowup['origin']; $upreply = $rowup['reply']; } } } hope the question isn't too complicated. basically if i remove the loop. the mysql num_rows works and returns a row. If i don't remove the loop it returns zero. Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 19, 2010 Share Posted August 19, 2010 Your foreach() loop is overwriting your original array $value with the first value in it - foreach($value as $value) { Use a different variable name to prevent your code from overwriting your own data. You also should not be executing a query inside of a loop. implode your original array $value into a string and use it in the mysql IN() statement to get all the matching rows in a single query. Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/#findComment-1101097 Share on other sites More sharing options...
MDanz Posted August 19, 2010 Author Share Posted August 19, 2010 nvm Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/#findComment-1101099 Share on other sites More sharing options...
PFMaBiSmAd Posted August 19, 2010 Share Posted August 19, 2010 Examples of how to use mysql functions are in the documentation - http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#function_in Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/#findComment-1101100 Share on other sites More sharing options...
MDanz Posted August 19, 2010 Author Share Posted August 19, 2010 i attempted below... but it's not working... what did i do wrong? $keyvalue = implode(" ",$value); foreach($value as $key) { $updatestable = mysql_query("SELECT * FROM `Stacks` WHERE keywords IN ('$keyvalue') ORDER BY id DESC LIMIT 1")or die (mysql_error()); $upnum = mysql_num_rows($updatestable); echo $upnum; if ($upnum==0){ $uporigin=0; } else { while($rowup = mysql_fetch_assoc($updatestable)) { $uporigin = $rowup['origin']; $upreply = $rowup['reply']; } } } Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/#findComment-1101102 Share on other sites More sharing options...
PFMaBiSmAd Posted August 19, 2010 Share Posted August 19, 2010 If you truly looked at the examples, you would notice that if your data is numeric, you would form a comma separate list of numbers and if your data is strings, you would form a comma separated list of single-quoted strings. Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/#findComment-1101105 Share on other sites More sharing options...
MDanz Posted August 19, 2010 Author Share Posted August 19, 2010 If you truly looked at the examples, you would notice that if your data is numeric, you would form a comma separate list of numbers and if your data is strings, you would form a comma separated list of single-quoted strings. i did look at the example. 1) i imploded $value and it echoes "testa testb". $keyvalue = implode(" ",$value); 2) $keyvalue now has 2 strings(testa testb)? how do i use that in the mysql IN clause then? I did this. you can't expect me to type out the strings myself because it should be automatic. WHERE keywords IN ('$keyvalue') Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/#findComment-1101107 Share on other sites More sharing options...
MDanz Posted August 19, 2010 Author Share Posted August 19, 2010 maybe i should rephrase it easier... the problem is mysql_num_rows is returning zero rows.. when it should return 1 row. the variable $value is an array with two strings (testa and testb)... when i remove the foreach loop and replace $key with 'testa' in the query it works(returns 1 row). how do i solve this?... both 'testa' and testb' have a row each in mysql. foreach($value as $key) { $updatestable = mysql_query("SELECT * FROM `Stacks` WHERE keywords LIKE '%$key%' ORDER BY id DESC LIMIT 1")or die (mysql_error()); $upnum = mysql_num_rows($updatestable); echo $upnum; } Quote Link to comment https://forums.phpfreaks.com/topic/211141-loop-problem-help/#findComment-1101111 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.