0

Here is my code:

$q = $db->prepare("SELECT * from slider WHERE page = :page ORDER BY index ASC");
$q->bindValue(':page', basename($_SERVER['PHP_SELF']));
$q->execute();

if ($q->rowCount() > 0){
    $result = $q->fetchAll(PDO::FETCH_ASSOC);

    for ($i=0; $i < $q->rowCount(); $i++) { 
        $path = $result[$i]['path'];
        echo "<div><img src=\"$path\"></div>";
    }
}

As is, there is no result that is being shown. If however I order by a different field, namely 'id' which is auto increment and the primary key of the table, I do get results that are ordered by the correct field.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • if there's no results, and the query didn't fail, then your `where` clause didn't match anything. so... .basic debugging: `var_dump($_SERVER['PHP_SELF'])` to see what you're testing with. – Marc B Jul 14 '15 at 16:54
  • Use pdoException to see bdd errors .. – crashxxl Jul 14 '15 at 16:57
  • @MarcB Although I understand what you're saying and I understand the reasoning behind it, whenever I remove `ORDER BY index ASC` the query is executed and I do get to see the results I expect. – Jayce Ardon Jul 14 '15 at 16:59
  • 1
    `index` is a mysql reserved word - https://dev.mysql.com/doc/refman/5.5/en/keywords.html – Sean Jul 14 '15 at 17:06

1 Answers1

1

'index' is a reserved word in MySQL. If you want to use it as a column name and in queries you must surround it with back ticks.

SELECT * from `slider` WHERE `page` = :page ORDER BY `index` ASC
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119