Help on search query with wildcards pls. - solved ty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

Help on search query with wildcards pls. - solved ty

Post 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
Last edited by rp on Sat Dec 11, 2004 1:04 pm, edited 1 time in total.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

Thanks Kettle_drum

Post by rp »

Thanks Kettle drum

Have removed the "white" spaces, but still no joy, is my SQL syntax correct?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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
rp
Forum Newbie
Posts: 21
Joined: Wed Nov 24, 2004 5:49 am

Thanks Kettle_drum

Post by rp »

Thanks, got it working now
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
Post Reply