When I try to sort the values ​​in descending order, I get an error:

Fatal error: Call to a member function prepare () on a non-object in /storage/sdcard0/www/shop/functions/functions_article.php on line 97

When sorting by ASC it works fine

Here is the request code:

 function article_get_articles($date){ global $pdo; $articles = ""; $stmt = $pdo -> prepare('(SELECT id FROM articles WHERE article_status=1) ORDER BY id DESC'); $stmt->execute(); foreach($stmt as $row) { $articles .= article_get_homeblock($row['id']); } return $articles; $pdo = null; } 

Addition:

Connection:

 $host = "localhost"; $db = "db"; $charset = "utf8"; $user = "user"; $pass = ""; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $pdo = new PDO($dsn, $user, $pass, $opt); 

Working f-tion:

 function article_get_author($data) { global $pdo; $stmt = $pdo -> prepare('SELECT article_author FROM articles WHERE id=? LIMIT 1'); $stmt->execute((is_array($data)?$data:[$data])); foreach ($stmt as $row) { return $row['article_author']; } } 
  • It can not be that you put ASC instead of DESC and everything works out as it should .... this is nonsense ........ and yes you have a bracket before the SELECT and after the status .. why are they? Probably an error in them .......... and why use prepare if no parameter is substituted? - Alexey Shimansky
  • I removed the brackets and everything works in the same way. Ie gives an error vseravno. - Ezdrael
  • What then instead of prepare to use? - Ezdrael
  • Check $pdo exists at all? Is there a connection? - Alexey Shimansky
  • There is. The remaining functions work fine. - Ezdrael

2 answers 2

If such an error occurs, the $pdo variable is not an object, and therefore it was either not created or not accessed in the function. In the code given by you there are no external errors.

You need to make sure that the $pdo object is available at the place you call the article_get_author() $pdo . Dump it

 <?php // Файл с созданием объекта $pdo require_once('connect.php'); // Файл с определением функции article_get_author(); require_once('author.php'); ... echo '<pre>'; print_r($pdo); echo '<pre>'; article_get_author($data); ... 

The result must be a PDO object dump.

 PDO Object ( ) 

If this is not the case, start moving the next line.

 echo '<pre>'; print_r($pdo); echo '<pre>'; 

above the code until you get close to creating the $pdo in the connect.php file or until you get the correctly created object. So you can localize the problem and find out what it is:

  • Somewhere overwritten global variable $pdo
  • The $pdo object $pdo not been initialized.

In general, the use of global variables is considered bad form, at any time you can wipe a variable or not connect it. This leads to hard-to-debug errors. Usually, when working with PDOs, they either create a base class (using or inheriting PDOs), from which all classes that need to access the database inherit, or put the database processing logic in a trait, which is mixed into classes.

  • I tried to do as you described and received: PDO Object () - Ezdrael
  • I checked everything and found a mistake. The variable $ pdo was overwritten in one of the functions. unset ($ pdo). Thank you - Ezdrael

Not. The $pdo variable does not store an object or this object does not have a prepare method.
Make sure that you actually connect to the database and put the resource in the global $pdo variable before calling this function.

  • For some reason, when I looked through print_r ($ pdo), it returned to me: PDO Object () - Ezdrael
  • @Ezdrael, it is important at what point you looked through print_r ($ pdo) - Grundy
  • @Grudy I watched right after global $ pdo; - Ezdrael