I'm connecting to sqlite in my db file, Then I created this function to access it from my functions file:
function db(){
require(getBasePath('db/connection.php'));
return $db;
}
This is what I'm doing and I get error node must be an int. My goal is take what what was posted and check it against my table in db and if user exists redirect to a booking page which I've created.
function login($request)
{
$username = $request['username'];
$password = md5($request['password']);
try {
$sql = db()->query("SELECT
*
FROM admin_user AS user
WHERE user.email = :username AND user._password = :pass",[
'username' => $username,
'pass' => $password
]);
$result = $sql;
} catch (Exception $e) {
echo "Unable to fetch row " . $e->getMessage();
exit;
}
if ($result->fetchAll()[0]['id'] < 1) {
throw new Exception('User does not exist');
}
$_SESSION['user_id'] = $result->fetchAll()[0]['id'];
return header('Location: ' . getAppUrl());
}
I've also tried using PDOStatement::bindParam
, PDOStatement::bindValue
, PDO::prepare
instead of query()
etc and still searching. ...
When I do the below, it works but I want to insert placeholders into an SQL statement instead of hardcoding what I'm checking for, I hope that makes sense.
function loadData()
{
try {
$sql = db()->query("SELECT
`user`.*,
`worker`.*,
`_address`.*
FROM `recipient` AS `user`
INNER JOIN `care_worker` AS `worker`
ON `worker`.`id` = `user`.`care_worker`
INNER JOIN `recipient_address` AS `_address`
ON `user`.`care_worker` = '1'");
$result = $sql;
} catch (Exception $e) {
echo "Unable to fetch row " . $e->getMessage();
exit;
}
return $result->fetchAll();
}
Please help, Thank you! :)