Fatal error: Uncaught PDOException: SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' inventory 'WHERE' Inv_User '=' gmarsi '' at line 1 in C: \ xampp \ htdocs \ qbc_web \ functions \ functions_contador.php: 14 Stack trace: # 0 C: \ xampp \ htdocs \ qbc_web \ functions \ functions_contador.php (14): PDOStatement-> execute () # 1 C: \ xampp \ htdocs \ qbc_web \ login \ profile.php (75): Count Elements ('Inv_User', 'inventory', 'Inv_User', 'gmarsi') # 2 {main} thrown in C: \ xampp \ htdocs \ qbc_web \ functions \ functions_contador.php on line 14
I get this error when generating the query, the code is this:
function contarElementos($element, $table, $condition, $purchased){
require(realpath($_SERVER["DOCUMENT_ROOT"]) .'/qbc_web/datos_conexion.php');
$consulta = "SELECT COUNT(:element) AS Contador FROM :table WHERE :condition = :purchased";
$result = $base -> prepare($consulta);
$result -> bindValue(':element', $element);
$result -> bindValue(':table', $table);
$result -> bindValue(':condition', $condition);
$result -> bindValue(':purchased', $purchased);
$result -> execute();
$sql_contador = $result->rowCount();
if($sql_contador < 1){
return 0;
}else{
while($fila = $result -> fetch(PDO::FETCH_ASSOC)){
return $fila['Contador'];
}
}
// echo $consulta;
}
The function basically what it does is to count the elements of X table taking into account the parameters that are introduced. As you can see in the error appears contarElementos('Inv_User', 'inventory', 'Inv_User', 'gmarsi')
which are the parameters that I want the function to use to query the database. Why is this?