Page 1 of 1

Help on search query with wildcards pls. - solved ty

Posted: Sat Dec 11, 2004 5:39 am
by rp
Hi folks

Can anyone help me figure this one out please.

I am trying to create a very simple search script using wildcards. User enters a keyword in a text field and submits that to this query

Code: Select all

<?php
$search = $_POST['search'];

$sqlqualsearch = "SELECT Qualification FROM allquals WHERE Qualification LIKE '% ".$search ." %'";
$rsqualsearch = mysql_query($sqlqualsearch);
while ($row = mysql_fetch_object($rsqualsearch))
{
echo $row['Qualification'];
} 

?>
The search needs only to look through 1 table and 1 field within that table. The text in the field to search is obviously longer than the key wor, but I would like all the rows returned that include the keyword $search.

For some reason my SQL doesn't work, yet the $search variable is passed via the $_POST etc..

Any help please, thanks in advance

Posted: Sat Dec 11, 2004 5:48 am
by kettle_drum
Try removing the white spaces from between % and $search. At the moment your looking for things like:

"com put er"

and not

"computer"

etc.

Thanks Kettle_drum

Posted: Sat Dec 11, 2004 5:55 am
by rp
Thanks Kettle drum

Have removed the "white" spaces, but still no joy, is my SQL syntax correct?

Posted: Sat Dec 11, 2004 6:03 am
by kettle_drum
I see it now. You are using mysql_fetch_object() which returns an object and not an array as you try to use it. Either change this function to mysql_fetch_assoc() or mysql_fetch_array() or use echo $row->Qualification

Thanks Kettle_drum

Posted: Sat Dec 11, 2004 6:20 am
by rp
Thanks, got it working now

Posted: Sat Dec 11, 2004 10:08 am
by John Cartwright
mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).
What advantages are there to returning rows as objects?

Posted: Sat Dec 11, 2004 12:22 pm
by ol4pr0
Speed wise maby ?

Dont know but php.net gave me that feeling. Identical to fetch_array and almost as fast as fetch_row