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.