Good day to all. The essence of the problem:

$find = '%fish%'; $db = new PDO(подключаемся); $sth = $db->prepare("SELECT id, title, url FROM news WHERE title LIKE ?"); $sth->execute(array($find)); $articles = $sth->fetch(PDO::FETCH_ASSOC); 

returns an array of Array ([id] => 12 [title] => fish fish fish [url] => ss.com). But only one line, and not all with similar entries (which are exactly). Tried and Bindvale and Bindparam before the executive, but to no avail. The only effective option was:

 $articles = $db->query("SELECT id, title, url FROM news WHERE title LIKE ".$find.""); 

but it’s not kosher: c

 $sth = $db->prepare("SELECT id, title, url FROM news WHERE title LIKE ?"); $sth->execute(array($find)); $articles = $sth->fetch(PDO::FETCH_ASSOC); 

This also returns the first row with the entry. Pleased with any comments.

  • instead of $ sth-> fetch (PDO :: FETCH_ASSOC); Try using $ sth-> fetchAll (PDO :: FETCH_ASSOC); - zippp

3 answers 3

$articles = $sth->fetch(PDO::FETCH_ASSOC);

You pulled the 1st row of the result.

maybe you should do it like this

 while($articles = $sth->fetch(PDO::FETCH_ASSOC)) { // тут делаем что нада с результирующим набором } 
  • oh yes it worked out thank you and I was withdrawing through forich and somehow it did not go - Godot
  • through forich did not go because fetch on 1 record gets. Here either fetch and while or fetchAll and foreach - Deyv Horni

$articles = $sth->fetchAll(PDO::FETCH_ASSOC);

  • empty array returns - Godot
  • one
    `$ sth = $ db-> prepare (" SELECT id, title, url FROM news WHERE title LIKE? "); $ sth-> execute (array ($ find)); $ articles = $ sth-> fetchAll (PDO :: FETCH_ASSOC); `If ​​it returns an empty array, then check the query without a condition. See, everything will work as it should. - lampa
  • This method, it seems, is not always there. Whether he, or some other depends on mysqlnd, and I, personally, did not work by default. - Oleg Arkhipov
 $sth = $db->prepare("SELECT id, title, url FROM news WHERE title LIKE ?"); $sth->execute(array('%'.$find.'%')); $sth-> setFetchMode(PDO::FETCH_ASSOC); while($row = $sth->fetch()){ // } 
  • I found this in many examples, but such a request does not return anything to me at all. upd: I apologize, did not notice. returns one. - Godot