I'm new to PDO and I'm struggling to use the 'where' statement in a prepared statement. Below is the code I tried. It returns Data could not be retrieved from the database.
Can anyone tell me what the issue is? Anything to try etc?
Appreciate it!
$user_id = 1; try { $query = $db->prepare(" SELECT title, img, id FROM listings ORDER BY id DESC WHERE userID = :user_id LIMIT 2"); $query->bindParam(':user_id', $user_id, PDO::PARAM_INT); $query->execute(); } catch (Exception $e) { echo "Data could not be retrieved from the database."; exit; } $data = $query->fetchAll( PDO::FETCH_ASSOC); return $data; UPDATE: I swapped the order & where clause, but it still returns nothing (as oppose to the previous error message).
$e, to see what was the problem?PDOException. Also, if you've changed your code, update your question