0
// connect to base
function db_connect () : PDO {
    static $db;
    if ($db == NULL){
        $db = new PDO ('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS,
                    [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
        $db->exec('SET NAMES UTF8');
    }
    return $db;
}
// prepair and execute sql
function dbQuery(string $sql, array $params = []) : PDOStatement{
    $db = db_connect();
    $query = $db->prepare($sql);
    $query->execute($params);
    dbCheckError($query);
    return $query;
}
//here is a func to return right category of lists with right OFFSET and LIMIT
function get_lists(int $idCat, int $idOffset) : array {
    $arr = [];
    if ($idCat <= 0 || $idCat >= 9)
        $sql = "SELECT name, file FROM listings LIMIT 2 OFFSET :idOffset;";
    elseif($idCat == 7)
        $sql = "SELECT name, file FROM listings ORDER BY time ASC LIMIT 2 OFFSET $idOffset;";
    elseif($idCat == 8)
        $sql = "SELECT name, file FROM listings ORDER BY time DESC LIMIT 2 OFFSET $idOffset;";
    else
        $sql = "SELECT name, file FROM listings JOIN categories USING (id_cat) WHERE id_cat = $idCat LIMIT 2 OFFSET $idOffset;";
    $query = dbQuery($sql, ['idOffset' => $idOffset]);
    $arr = $query->fetchAll();
    return $arr;
}

So I have this error when I open php file:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''0'' at line 1

And I know even why and where is mistake, but I dont understand what it wants me to do :(

The mistake is on line

$sql = "SELECT name, file FROM listings LIMIT 2 OFFSET :idOffset;";

once I put mask, if I put straightly variable $idOffset then it works all fine as in other sql requests in this function. Why is that? How to put mask?

if you need any additional info let me know. Will appreciate your help!

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • What is `mask`? You need to cast and bind as an integer. – user3783243 Apr 04 '20 at 02:13
  • 1
    You need to use `bindValue` as described in the duplicate to tell the driver that the value you are passing for `:idOffset` is an integer. Also you need to replace `$idOffset` in your other queries with `:idOffset`. – Nick Apr 04 '20 at 02:24
  • $query->execute($params); thats a line where its binding emm Ill try tho with bind method but it seams result will be the same – Eldar Tailov Apr 04 '20 at 02:59

0 Answers0