0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I'm running a query:

$result = mysql_query("SELECT *, CONCAT(fname, ' ', lname) AS fullname FROM `users` WHERE fullname LIKE '%$keyword%' ");

while ($myrow = mysql_fetch_array($result)) {
  $fname = $myrow['fname'];
  echo $fname;
}

fullname consists of two names separated with a space.
And I have an error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in localhost\search.php on line 8

How i can fix this error?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Koiw
  • 21
  • 1
  • 5
  • Can you please write the output of following code: echo mysql_error(); ideally place this before while loop. – Cem Kalyoncu Jun 11 '11 at 15:03
  • The SQL itself seems ok. To debug this, echo the full SQL-Statement in your mysql_query() and add the output of mysql_error(). – Bjoern Jun 11 '11 at 15:05
  • thanks. i did this. write: Unknown column 'fullname' in 'where clause' – Koiw Jun 11 '11 at 15:08
  • 1
    always check $result!==FALSE before looping on the result. And add some error handling code in case of FALSE, you'll get a more robust code (this does not fix the SQL error but at least you'll get better error codes). – regilero Jun 11 '11 at 15:08
  • When you have new information don't put in comments, edit the question, write EDIT: then put whatever new information there. This will help you (question will be pushed to top in the list) and us (we will not miss important information). – Cem Kalyoncu Jun 11 '11 at 15:11

2 Answers2

1

Ok I think I saw your mistake. In MySQL you cannot use a named column (fullname) in where clause. Because when where clause is executed, the rows are not fetched yet. Following should not cause any problems:

$result = mysql_query("
  SELECT 
    *, 
    CONCAT(fname, ' ', lname) AS fullname 
  FROM users
  WHERE 
    CONCAT(fname, ' ', lname) LIKE '%$keyword%'
"); 

while ($myrow = mysql_fetch_array($result)) { 
   $fname = $myrow['fname']; echo $fname; 
}
Cem Kalyoncu
  • 13,474
  • 4
  • 40
  • 60
0

Why don't use Full text for this Table ?. If you need to search the record in your table, in this mode, the full text search is very powerful and faster.

twcms
  • 1