user2257002
user2257002

Reputation: 1

SQLite3 querying error

I need some help with my code. I get this error:

PHP Warning: SQLite3::query(): Unable to prepare statement: 1, near =: syntax error in /var/www/html/image.php on line 16

PHP Fatal error: Uncaught Error: Call to a member function fetchArray() on boolean in /var/www/html/image.php:17`

Stack trace:

0 {main}

thrown in /var/www/html/image.php on line 17

This is my code:

<?php
error_reporting(E_ALL ^ E_NOTICE);
Header("Content-type: image/png");
$im = imagecreate(304, 214);
$blanco = imagecolorallocate($im, 255, 255, 255);
imagerectangle($im, 0, 0, 304, 214, $blanco);
$rojo = imagecolorallocate($im, 255, 0, 0);
$verde = imagecolorallocate($im, 0, 255, 0);
$azul = imagecolorallocate($im, 0, 0, 255);
$amarillo = imagecolorallocate($im, 255, 255, 0);
$violeta = imagecolorallocate($im, 46, 49, 146);
$naranja = imagecolorallocate($im, 242, 101, 34);
$negro = imagecolorallocate($im, 0, 0, 0);

if ($db = new SQLite3 ('/var/www/html/db/SeriesDb.sqlite')) {
    $q = $db-> query("SELECT * FROM tbl_Series where id "= .$_REQUEST["id"]);
    while ($row = $q-> fetchArray()) {
        $id = $row[0];
        $dayweek = date("N", strtotime($row[1]));
        $serie = explode(" ",$row[2]);
    }
} else {
    die("error");
}

switch ($dayweek) {
    case 7: $color = $rojo;
        break;
    case 6: $color = $naranja;
        break;
    case 5: $color = $amarillo;
        break;
    case 4: $color = $verde;
        break;
    case 3: $color = $azul;
        break;
    case 2: $color = $violeta;
        break;
    case 1: $color = $negro;
}
$j = 0;
$y = 0;
$x = 0;

for ($i = 0; $i < 70; $i++) {
    $j++;
    if ($j > 10)
        $j = 1;
    $x = 30 * $j - 28;
    $y = $i % 10 == 0 ? 2 + ($i / 10) * 30 : $y;
    imagerectangle($im, $x, $y, $x + 30, $y + 30, $negro);
    if (in_array($i + 1, $serie))
        imagefilledrectangle($im, $x + 1, $y + 1, $x + 30 - 1, $y + 30 - 1, $color);
}
Imagepng($im);
Imagedestroy($im);
$db->close();

What could the problem be?

Upvotes: 0

Views: 3054

Answers (2)

cartant
cartant

Reputation: 58400

The = sign appears to be outside the string literal to which you are concatenating $_REQUEST["id"]. It should be something like this:

$q = $db->query("SELECT * FROM tbl_Series where id = " . $_REQUEST["id"]);

However, it would be better to avoid the concatenation and use a prepared statement:

$stmt = $db->prepare("SELECT * FROM tbl_Series where id = :id");
$stmt->bindValue(":id", $_REQUEST["id"], SQLITE3_TEXT);
$q = $stmt->execute();

Upvotes: 1

Sandesh Jain
Sandesh Jain

Reputation: 742

Its looks like your query is not generating properly. It is recommended to use prepare statement to add run time params into query. you can go with this tutorial.

Upvotes: 0

Related Questions