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


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

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.