0

I want to search in the mysql database. I want the search sent for search to be searched in 2 columns in the table. But I did not get results with this code.

$Ara = $db->prepare("SELECT * FROM urun_bilgileri WHERE (urun_adi LIKE ? OR urun_kodu LIKE ?)");
$Ara->execute(array('%'.$Gelen.'%'));
while($Liste = $Ara->fetchAll(PDO::FETCH_ASSOC)){;
     $toplam_icerik = $Ara->rowCount();
}

where is the problem?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    You need to have one value in your array for each parameter in the query. You have two parameters and only one value. Try changing `$Ara->execute(array('%'.$Gelen.'%'));` to `$Ara->execute(array('%'.$Gelen.'%', '%'.$Gelen.'%'));` – Nick Oct 25 '18 at 06:42
  • Try this `$Ara = $db->prepare("SELECT * FROM urun_bilgileri WHERE (urun_adi LIKE :first OR urun_kodu LIKE :second)"); $Ara->bindValue(':first', '%' . $Gelen . '%', PDO::PARAM_STR);$Ara->bindValue(':second', '%' . $Gelen . '%', PDO::PARAM_STR); $Ara->execute(); $data = $Ara->fetchAll();` – Gopal Sharma Oct 25 '18 at 06:46

3 Answers3

1

try with this,need to pass two values

$Ara = $db->prepare("SELECT * FROM urun_bilgileri WHERE (urun_adi LIKE ? OR urun_kodu LIKE ?)");
$Ara->execute(array('%'.$Gelen.'%','%'.$Gelen.'%'));
while($Liste = $Ara->fetchAll(PDO::FETCH_ASSOC)){;
     $toplam_icerik = $Ara->rowCount();
}
Rp9
  • 1,955
  • 2
  • 23
  • 31
1

You use two placeholders in the query, but pass only one parameter. You must pass two parameters, for example:

$keyword = '%'.$Gelen.'%';
$Ara = $db->prepare("SELECT * FROM urun_bilgileri WHERE (urun_adi LIKE ? OR urun_kodu LIKE ?)");
$Ara->execute(array($keyword, $keyword));

or you can use named placeholders. For example:

$keyword = '%'.$Gelen.'%';
$Ara = $db->prepare("SELECT * FROM urun_bilgileri WHERE (urun_adi LIKE :urun_adi OR urun_kodu LIKE :urun_kodu)");
$Ara->execute(array(':urun_adi' => $keyword, ':urun_kodu =>$keyword));
Maksym Fedorov
  • 6,383
  • 2
  • 11
  • 31
0

Enable PHP error_reporting and PDO error_reporting than PHP tells you this within an error message.

error_reporting(-1);
ini_set('display_errors', true);

and http://php.net/manual/de/pdo.error-handling.php

hausl
  • 160
  • 16