Jump to content

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\search.inc.php on line 13


phpperson

Recommended Posts

Ok so i'm making a working search but it not working like when i type C it's supposed to give me the peoples first name with the letter C 

but it's giving me the error message in the title.

Here's The Code for my file "thephp.php":

<?php
   
?>
 
<!doctype html>
<html>
<head>
 
 
<script type="text/javascript">
 function findmatch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
 
 
 
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
 
 
 
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'search.inc.php?search_text='+document.search.search_text.value, true);
xmlhttp.send();
 
 }
  
</script>
</head>
<body>
 
  <form id="search" name="search">
  Type a name:
  <input type="text" name="search_text" onKeyup="findmatch();">
 
 </form>
<div id="results"></div>
 
</body>
</html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
And here is my code for the file "search.inc.php":
<?php
 
   include 'datebase.inc.php';
  if(isset($_GET['search_text'])) {
 echo $search_text = $_GET['search_text'];
    
  
  }
  
  $query = "SELECT `firstname` FROM `users` LIKE '".mysql_real_escape_string($search_text)."%'";
$query_run = mysql_query($query);
 
while($query_row = mysql_fetch_assoc($query_run)) {
echo $firstname = $query_row['firstname'].'<br>';
}
?>
 

 

If you read the FAQ for this forum first you would know that you get that error when the query fails.

 

In this case you have a syntax error. You need a WHERE clause. Try

  $query = "SELECT `firstname` FROM `users` WHERE `firstname` LIKE '".mysql_real_escape_string($search_text)."%'";

Next time you post code, use the forum's [ code ]..[ /code ] tags or use the <> button in the toolbar

Archived

This topic is now archived and is closed to further replies.

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