Search code examples
mysqlpdowhere-clausemysql-error-1064

PDO error in the "WHERE"


what is wrong with this code, It says me this, but I don't see what is bad :

Error : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE B='achillea_millefolium_credo'' at line 1

Here is my code :

(before) for info : if we echo "$all_db_types_associations['nom_url']" it would echo "B" (the B column in my database)

and if we echo "$plante" it would echo "achillea_millefolium_credo" (the name of a plant)

$plante_undecoded = htmlspecialchars($_GET["plante"]);
$plante = htmlspecialchars_decode(htmlspecialchars_decode($plante_undecoded));

try
{
    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
    $bdd = new PDO('mysql:host=' .$host .';dbname=plantes', $username, $password, $pdo_options);

    $bdd->query("SET NAMES 'utf8'");

    $pdo = 'SELECT * FROM ' .$current_db_name ." ORDER BY " .$all_db_types_associations['nom_bot'] ." WHERE " .$all_db_types_associations['nom_url'] ."=? ";
    //echo $pdo ." <br />";

    $reponse = $bdd->prepare($pdo);
    $reponse->execute(array($plante));



    while ($donnees = $reponse->fetch())
    {
        //things here...
    }

    $reponse->closeCursor();
}

catch(Exception $e)
{
    die('Erreur : '.$e->getMessage());
}

is it because my columns name in MySQL are in caps ("B")? else... what da f?


Solution

  • Your ORDER BY clause needs to come after your WHERE clause.

    SELECT col FROM table WHERE condition ORDER BY col