Basic query not working: where A is LIKE '%B%'...

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

mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Basic query not working: where A is LIKE '%B%'...

Post by mikosiko »

I did use the word "Ford" only following your example... the query is going to find any record where the "keyword" field value is present in your $row->title string

regarding your error... echo your query again to see if it is correct or not... and also I suggest you to change the usage of $row here

Code: Select all

while ($row = mysql_fetch_object($result))


to eliminate the conflict with your Object $row
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic query not working: where A is LIKE '%B%'...

Post by simonmlewis »

Ok.
The $row is for show here only, as it is named something else - but in essence, it should work as I have written it.
SELECT * FROM relate WHERE INSTR(Ford F150, keyword) > 0
This is what it echos.
Error remains.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Basic query not working: where A is LIKE '%B%'...

Post by mikosiko »

SELECT * FROM relate WHERE INSTR(Ford F150, keyword) > 0
and what did you do to fix that?... the error is very evident... debug your code
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic query not working: where A is LIKE '%B%'...

Post by simonmlewis »

Sorry what?
$title or $row->title, is 'Ford F150'.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Basic query not working: where A is LIKE '%B%'...

Post by mikosiko »

simonmlewis wrote: is 'Ford F150'.
yes... and 'Ford F150' is NOT the same than Ford F150 ...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic query not working: where A is LIKE '%B%'...

Post by simonmlewis »

Heavens sorry, I meant it without the apostophies. Yes, I know that's vital.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic query not working: where A is LIKE '%B%'...

Post by simonmlewis »

Could you do something like this:
select everything from the relate db.
Echo the keyword.
Is $keyword similar to $title - if YES, show something.
And do this for all keywords.

So rather than querying the keywords in the SQL statement itself, extract all keywords, and then only show if they match.

But how would you do that in PHP - finding if a word is in a statement? ie. is keyword 'ford' in statement 'ford 150 truck'?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Basic query not working: where A is LIKE '%B%'...

Post by mikosiko »

it is possible, but no efficient....

why select all the records and then post-process them to chose the matching one?.... I don't see reason to do something like that....

what is wrong with the provided solution (it will select just the matching records)? did you tested/fixed the missing single quotes?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic query not working: where A is LIKE '%B%'...

Post by simonmlewis »

Yes and posted the error results back. All it did was to produce ALL records from the db.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic query not working: where A is LIKE '%B%'...

Post by simonmlewis »

relate WHERE INSTR(ford 150, keyword) > 0
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\phpMyAdmin\site\includes\product.inc on line 438

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\product.inc on line 441
This is the error I get, after echoing the query.
The $row etc are all set correctly.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Basic query not working: where A is LIKE '%B%'...

Post by mikosiko »

simonmlewis wrote:
relate WHERE INSTR(ford 150, keyword) > 0
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\phpMyAdmin\site\includes\product.inc on line 438

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in C:\xampp\phpMyAdmin\site\includes\product.inc on line 441
This is the error I get, after echoing the query.
The $row etc are all set correctly.
and I did answer and show you which was the error.... let point it out again
WHERE INSTR(ford 150, keyword) > 0

you don't see that ford 150 there doesn't have single quotes surrounding it? ... for that reason your query is INVALID... hence the errors that you get after that. what you must do is to be sure that the STRING that you are using is enclosed in single quotes... simple as that
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Basic query not working: where A is LIKE '%B%'...

Post by simonmlewis »

Ok, well your initial code did not have quotes around it either.
However, I have put them in and BINGO! I've tried it on various products where there are keywords and arnt - and works perfect.

So thank you for your hard work in helping me.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply