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

My first instinct would be to ask whether you want the $key variable to contain the key of the array or the value. If you want the former your foreach will need to look like this:

<?php
//Php doesn't implicitly assume you want the key if you call the variable key
foreach($value as $key => $dummy) {
...
?>

If this is not the case, why not try some simple debugging? It can often uncover small issues & typos that you overlook with a quick glance. How about something like this first:

foreach($value as $key) {
echo 'Key: '.$key."<br>\n";
...
}

Make sure that it outputs:

Key: testa
Key: testb

If it doesn't then there's your problem! Otherwise, let's keep going. The next thing I would try is echoing out each of the queries and trying them in PhpMyAdmin. Just looking at the query sometimes can reveal the problem.
If you don't find the problem here, try reformatting your code; it might not help, but its worth a try:

foreach($value as $key) {
    $r = mysql_query("SELECT * FROM Stacks WHERE keywords LIKE '%".$key."%'");
    echo 'Key "'.$key.'": '.$upnum."<br>\n";
}

Let us know how it goes!

EDIT: I forgot to ask: Do you have PHP set to report all errors? Trying the query in PhpMyAdmin will show any errors with the MySQL, but you can also try enabling errors in PHP (so you can see if MySQL triggers any).

//We'll ignore E_STRICT for now :)
error_reporting(E_ALL);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.