PDO execute() then fetchall() with bound paramaters. I can't get it to work -
i new pdo, , trying
function executesqlquery($connection, $sqlquery, $paramsarray) { try { $sql = $connection->prepare($sqlquery); $sql->setfetchmode(pdo::fetch_assoc); $sql->execute($paramsarray); $result = $sql->fetchall(pdo::fetch_assoc); } ... etc
$result
empty array.
can tell me doing wrong?
$connection
valid (i check (not shown), throw exception if not)
paramsarray = array('modem_id' => '1'); $sqlquery = "select vehicle_id vehicles modem_id=":modem_id"
[update] requested, here code in more detail.
function executesqlquery($connection, $sqlquery, $paramsarray) { try { $sql = $connection->prepare($sqlquery); $sql->setfetchmode(pdo::fetch_assoc); $sql->execute($paramsarray); $result = $sql->fetchall(pdo::fetch_assoc); } ... etc nothing matters after point in function, things have gone wrong - $result null catch (exception $e) { echo $e->getmessage(); // code not reached } } // executesqlquery() $connection = connecttodatabase(); // need code that? works, ide shows pdo object $connection->setattribute(pdo::attr_errmode, pdo::errmode_exception); $queryparams = array('modem_id' => "1"); $sqlquery = 'select vehicle_id ' . database_name . '.vehicles modem_id=":modem_id"'; $result = executesqlquery($connection, $sqlquery, $queryparams);
you shouldn't quote parameter did:
$sqlquery = 'select vehicle_id ' . database_name . '.vehicles modem_id=":modem_id"';//remove quotes
change following:
$sqlquery = 'select vehicle_id ' . database_name . '.vehicles modem_id=:modem_id';
Comments
Post a Comment