Search by LIKE

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Search by LIKE

Post 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?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

what's the code for where and how you're doing the query itself and what error-message are you getting back?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Fixed it

Post 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'] . "%'";
?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

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