ShadowZzz
ShadowZzz

Reputation: 415

PDO search database using LIKE

I am trying to make a small search function to look at database using this code:

$searchQ = 'Li';
$query = $connDB->prepare('SELECT * FROM topic WHERE topic_name LIKE '."':keywords'");
$query->bindValue('keywords', '%' . $searchQ . '%');
$query->execute();  

if (!$query->rowCount() == 0) {
    while ($results = $query->fetch()) {
        echo $results['topic_name'] . "<br />\n";
    }
} else {
    echo 'Nothing found';
}

This return all of the items in database, not just the ones that are alike,

I then ran this SQL query:

SELECT * FROM topic WHERE topic_name LIKE '%Li%';

and this ran as expected and returned the required result.

What am I missing?

Upvotes: 5

Views: 31279

Answers (4)

Mahdani Ismail
Mahdani Ismail

Reputation: 21

showdev thanks you tutorial

SQL

CREATE TABLE IF NOT EXISTS 'news' 
(`id` int(9) NOT NULL auto_increment, 
  `title` text NOT NULL, 
  PRIMARY KEY  (`id`)
) 
ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
insert into `items` (`id`, `title`) VALUES
(1, 'news title'),
(2, 'news title 2');

PHP

$connection = new PDO('mysql:host=localhost;dbname=YOUR_DATABASE_NAME','root','');
if (isset($_GET['store'])) {
$store = $_GET['store'];
if(!empty($store)){//condition you the remove karakter
    $q=$connection->prepare("SELECT title FROM tbl_nes WHERE title LIKE :store");
    $q->bindValue(':store','%'.$store.'%'); 
    $q->execute();
    while ($r = $q->fetch(PDO::FETCH_OBJ)) { 
        echo $r->title,"<br>";
    }         
}
} 

OUTPUT

1.news title
2.news title 2 ....

Upvotes: -1

Mihai
Mihai

Reputation: 26784

You can only bind data literals. Try

 $searchQ = '%Li%'; OR  $searchQ = '%'.Li.'%';
$query = $connDB->prepare('SELECT * FROM topic WHERE topic_name LIKE :keywords');
$query->bindValue('keywords', $searchQ );

Upvotes: -1

showdev
showdev

Reputation: 29168

Remove the quotes from the placeholder and add a colon before your bind reference:

$query = $connDB->prepare('SELECT * FROM topic WHERE topic_name LIKE :keywords');
$query->bindValue(':keywords', '%' . $searchQ . '%');

Here's my text example:

SQL

CREATE TABLE IF NOT EXISTS `items` (
  `id` mediumint(9) NOT NULL auto_increment,
  `name` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


INSERT INTO `items` (`id`, `name`) VALUES
(1, 'apple'),
(2, 'orange'),
(3, 'grape'),
(4, 'carrot'),
(5, 'brick');

PHP

$keyword='ap';
$sql="SELECT * FROM `items` WHERE `name` LIKE :keyword;";
$q=$dbh->prepare($sql);
$q->bindValue(':keyword','%'.$keyword.'%');
$q->execute();
while ($r=$q->fetch(PDO::FETCH_ASSOC)) {
    echo"<pre>".print_r($r,true)."</pre>";
}

Output

Array
(
    [id] => 1
    [name] => apple
)
Array
(
    [id] => 3
    [name] => grape
)

Upvotes: 18

Moeed Farooqui
Moeed Farooqui

Reputation: 3622

You need to pass a single variable to bind_param not a concatenated string, because parameter two must be a reference not a value.

$keyword_to_search = '%' . $searchQ . '%';
$query->bindValue('keywords', $keyword_to_search); 

Upvotes: 0

Related Questions