Jump to content

loop problem help


MDanz

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];
         
          }
     }
         
      } 

Link to comment
Share on other sites

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') 

 

 

Link to comment
Share on other sites

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;
  }


  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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