There is a script that works correctly on the local machine, but nothing happens when installing it on the server in the product.tpl file (cms OpenCart). The script takes the price from the table, taking into account the id of the product. Error code does not display any. Thanks for the help.

<?php $host = 'хост'; $db = 'имя бд'; $user = 'юзер нейм'; $pass = 'пароль'; $charset = 'utf8'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $pdo = new PDO($dsn, $user, $pass, $opt); $stmt = $pdo->prepare('SELECT price FROM oc_product WHERE product_id = ?'); $stmt->execute([$_GET['id']]); foreach ($stmt as $row) { echo 'price '.$row['price'] . "\n"; } ?> 

  • did not help. I tried: while (($ row = $ stmt-> fetch (PDO :: FETCH_COLUMN))! == false), while ($ row = $ stmt-> fetch (PDO :: FETCH_COLUMN)), while ($ row = $ stmt-> fetch (PDO :: FETCH_ALL)). Any more suggestions? - Vlad Yudkin
  • var_dump($stmt->execute([$_GET['id']])); which will output if put instead of $stmt->execute([$_GET['id']]); ? - Visman
  • @visman with a while loop ($ row = $ stmt-> fetch ()) displays true - Vlad Yudkin
  • Then before the loop, do var_dump($stmt->fetchAll()); and most likely you will see an empty array :) - Visman

1 answer 1

$ _GET ['id'] did not cause anything, correctly $ _GET ['product_id']

  • a bit off topic: opencart is a good model for working with DB. Both mysqli and pdo are implemented. Why are you describing a new connection? your site will not benefit from this. And it's very bad that it is still in the tpl file. with respect. - Kirill Korushkin
  • @KirillKorushkin yes, I agree with you about the basic functions of the platform. I rewrote the request. That example that in the HARDWARE became by the local machine which did not have cms, but had unloaded dd from opencards. Thanks for the recommendations) - Vlad Yudkin