Hello! The function receives a parameter through the address bar and makes a selection from the database. If I enter the request with specific numbers in phpmyadmin, then the request displays what you need. And the function returns an empty array:

function article_block($category){ try { $dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', HOST, DB); $pdo = new PDO($dsn, USER, PASS); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "<p>Запрос на выборку данных из бызы не прошёл. Напишите об этом администратору <a href='mailto:admin@torawhite.ru'>admin@torawhite.ru</a>.</p><br>ERROR: " . $e->getMessage(); exit; } $sql = '(SELECT article_id, article_cat_id, article_par_id, article_title, article_img, article_author, article_author_card, article_description, article_date, article_views FROM articles WHERE article_cat_id = :cat) UNION (SELECT article_id, article_cat_id, article_par_id, article_title, article_img, article_author, article_author_card, article_description, article_date, article_views FROM articles WHERE article_cat_id IN ( SELECT cat_id FROM magazine_left_sidebar WHERE parent_id = :cat ))'; $data = $pdo->prepare($sql); $data->bindValue (':cat', $category); $data->execute(); $article_block = $data->fetchAll(PDO::FETCH_ASSOC); return $article_block; } 

I can not understand what's wrong ...

  • I would try to remove the extra brackets and make two parameters, what the hell is not joking - etki
  • If bindValue is used, then it is desirable to specify the type of the value being bound - $data->bindValue (':cat', $category, PDO::PARAM_INT); In general, you can exclude bindValue: $data->execute(array(":cat"=>$category)); And what if, after $data->execute to make var_dump($data) - see what is there to extract? - Boris
  • @Etki, it worked only like this: $ data-> bindValue (': cat', (int) $ category, PDO :: PARAM_INT); But it does not work correctly - for any parameter it extracts the same thing. If the request is copied directly to phpmyadmin, then everything works fine - torawhite
  • @boris_U, does not work, unfortunately. Forcibly set the value to the same function - 1, for example, everything works! What is not so ((( - torawhite
  • Understood $ category = $ _GET ['category']; I did not think that it was so important, as it turned out ... - torawhite

1 answer 1

From the documentation:

"You must select unique pseudo-variable names for each value that must be passed to the query when calling PDOStatement :: execute ()." You cannot use one pseudo-variable in the query more than once, unless the emulation mode is enabled.

Note: Parameter markers are only data directly. No part of the data, no special words, no identifiers, no other part of the request can be passed through the parameters. For example, you cannot bind multiple values ​​to a single parameter for a SQL IN () expression. "