I was following a tutorial and i copied word by word i don't know why i get the error.
Error >> Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/*****/public_html/download/url/search.php on line 36

<?php
		
		 $results = $_GET['results'];
		 $terms = explode(" ", $results);
		 $query = "SELECT * FROM search WHERE keywords='$term1'";
		 
		 foreach ($terms as $each) {
				$i++;
				if ($i == 1)
					$query .= "keywords LIKE '%$each%' ";
				else 
					$query .= "OR keywords LIKE '%$each%' ";
			}
			
			// connect
			mysql_connect("localhost", "irnm_tutadmin", "731995");
			mysql_select_db("irnm_tutorials");
			
			$query = mysql_query($query);
			$numrows = mysql_num_rows($query);
			if ($numrows > 0) {
			
				while ($row = mysql_fetch_assoc($query)){
					$id = $row['id'];
					$title = $row['title'];
					$description = $row['description'];
					$keywords = $row['keywords'];
					$link = $row['link'];
					
					echo "<h2><a href='$link'>$title</a></h2>
					$description<br /><br />";
				}
			}
			else
				echo "No results found for \"<b>$results</b>\"";
			
			
			// disconnect
			mysql_close();
	
	?>

Dani AI

Generated

The warning means your query failed and mysql_query() returned false, so mysql_num_rows() got a non‑resource. In this thread the immediate cause is a malformed SQL string: the script starts with a hardcoded WHERE fragment using an undefined $term1, then the loop appends additional keywords LIKE ... clauses without fixing the original fragment or adding proper spacing/operators. That produces invalid SQL. and correctly flagged the undefined/incorrect WHERE; is right to suggest using a separate variable name for the query string vs the query result; and is right to recommend checking the database error message to see exactly why the query failed.

Quick troubleshooting checklist

  • Echo the final SQL before executing and paste it into your DB client to see the syntax.
  • Always check the DB error (use mysqli_error() or PDO exceptions) immediately after mysqli_query()/prepare().
  • Initialize counters (e.g. $i = 0) or use a boolean/array technique instead of naked $i++.
  • Rename variables so $sql = string and $result (or $res) = query resource.
  • Sanitize input to avoid SQL injection.

Safer, minimal pattern (uses mysqli; not the original mysql_* calls):

$terms = preg_split('/\s+/', trim($_GET['results']));
$link = mysqli_connect('host','user','pass','db');
$conds = [];
foreach ($terms as $t) {
  $t = mysqli_real_escape_string($link, $t);
  $conds[] = "keywords LIKE '%$t%'";
}
$sql = 'SELECT id,title,description,keywords,link FROM search' . (count($conds) ? ' WHERE '.implode(' OR ', $conds) : '');
$res = mysqli_query($link, $sql) or die('Query error: '.mysqli_error($link));

Notes: prefer prepared statements (PDO or mysqli) for real apps, consider full‑text search for multiword lookups, and avoid the old mysql_* extension (removed from modern PHP). Initializing variables and checking the DB error will immediately show the exact syntax problem.

Recommended Answers

All 5 Replies

what is $term1 in your query(line 5)? Where did you define $term1?

Replace your line no 5

$query = "SELECT * FROM search WHERE keywords='$term1'";

with

$query = "SELECT * FROM search WHERE ";

You're putting the result in $query on line 19, which is a bit strange to me. It shouldn't break things, but it's more clear if you rename that variable to $result.

Also always try to habit of mysql_error so once there is something wrong with query your code wont execute further.

$rs=mysql_query($sql) or die( '<br /><strong>Your Query: </strong>'.$sql.'<br /><br /><strong>Error: </strong>'.mysql_error());

There's a problem with your SQL syntax.

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.