Page 1 of 1

Search by LIKE

Posted: Fri Oct 01, 2004 10:23 am
by AliasBDI
I have a query that queries for 1 column ("common") with three separate variables ("search1, search2, search3"). Here is my query with actual words instead of variables:

Code: Select all

<?php
SELECT * FROM con_doctrines WHERE common LIKE '%word1%' OR common LIKE '%word1%' OR common LIKE '%word2%'
?>
This works fine. The "word1, word2, word3" will actually be variable results.

So I tried making the variables which are pulled from another query record ("detailsDoctrine") and then do the query above with the variables:

Code: Select all

<?php
$search1 = "%" . $row_detailsDoctrine['search1'] . "%";
$search2 = "%" . $row_detailsDoctrine['search2'] . "%";
$search3 = "%" . $row_detailsDoctrine['search3'] . "%";

SELECT * FROM con_doctrines WHERE common LIKE $search1 OR common LIKE $search2 OR common LIKE $search3
?>
That does not work. What am I doing wrong?

Posted: Fri Oct 01, 2004 10:27 am
by patrikG
what's the code for where and how you're doing the query itself and what error-message are you getting back?

Fixed it

Posted: Fri Oct 01, 2004 10:34 am
by AliasBDI
My % needed outer single-quotes. Here is how they should look:

Code: Select all

<?php
$search1 = "'%" . $row_detailsDoctrine['search1'] . "%'";
$search2 = "'%" . $row_detailsDoctrine['search2'] . "%'";
$search3 = "'%" . $row_detailsDoctrine['search3'] . "%'";
?>

Posted: Fri Oct 01, 2004 10:47 am
by patrikG
I meant line 6 - if you have a syntax like

Code: Select all

$query= 'select * from table where search like $search';
you'd get an error.

But those are wild guesses until you post what error you're getting.