0

I'm currently doing some PDO for a school project and I don't seem to find the errors with my code. MySql says "check the manual that corresponds to your MySQL server version for the right syntax to use near 'like WHERE no_image = '0'' at line 1" ... But I have check online and this is how most people do it. The goal of my code is as follow

-Create an empty array of 10 elements -Find the number of likes a photo has -Place the photo ID inside the array -Re-use the PDO to switch the photo ID with its name (the same as in the server folder). -Put them in a $_SESSION to be able to for() the photo on the html page.

<?php

$table = array(
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    );
$imageNo = 1;
$total_like  = 0;
try
{
    include('database_connect.php');
    while ($imageNo > 0)
    {
        $reader = $Cnn->prepare('SELECT COUNT(no_membre) AS total_like FROM like WHERE no_image = :no');
        $reader->execute(array('no' => $imageNo));
        $image = $reader->fetch(); 

        $total_like = $image['total_like'];

        if ($total_like > 0) {
            for ($i = 0; $i < 9; $i++) {
                if ($total_like > $array[$i]) {
                    if ($total_like < $array[$i+1]) {
                        $array[$i] = $imageNo;
                        $i = 10;
                    }
                }

            }
        }
        else {
            $imageNo = 0;
        }
    $reader->closeCursor();
    }
}
catch (PDOexception $erreur)
{
    echo 'Error' . $erreur->getMessage();
}

try
{
    for ($i = 1; $i <= 9; $i++)
    {
        $reader = $Cnn->prepare('SELECT nom FROM image WHERE no_image = :no_image');

        $reader->execute(array('no_image' => $table[$i]));

        $image = $reader->fetch();

        $table[$i] = $image['nom'];
        }
    $reader->closeCursor();
}
catch (PDOexception $erreur)
{
    echo 'Error' . $erreur->getMessage();
}

$_SESSION['table'] = $table;

?>

Thanks you very much. If you have any tips, please feel free to tell me. I'm new to using PDO so yeah, I know my code might be messy and not the best.

  • `FROM like` https://dev.mysql.com/doc/refman/5.5/en/keywords.html --- http://php.net/manual/en/pdo.error-handling.php and make sure you started the session. check for errors http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Dec 13 '15 at 04:22
  • `like` is a MySQL reserved keyword. For best results, avoid naming tables with MySQL keywords. As a workaround, add backticks before and after the table name: `...AS total_like FROM \`like\`...` – George Cummins Dec 13 '15 at 04:39
  • Thanks Georges, :) Have a good night – Gabriel Roy Dec 13 '15 at 04:59
  • If you got the answer as you mentioned above, please post it here and accept your own answer. Don't keep the question open. Thanks. – Tristan Dec 13 '15 at 05:59

0 Answers0