PDO search database using LIKE

前端 未结 4 1508
攒了一身酷
攒了一身酷 2020-12-18 08:43

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          


        
4条回答
  •  别那么骄傲
    2020-12-18 08:46

    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,"
    "; } } }

    OUTPUT

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

提交回复
热议问题