This is the update code
But I got the error : FATAL ERROR (Call to undefined method User::fetch_assoc())
this is my method code
public function getAllUsers(User $user)
{
$stmt = $this->conn->prepare("SELECT u.user_id, u.email, u.name, u.phone, u.picture, s.status
FROM ".$this->table_name." as u, status as s
WHERE u.status_id = s.status_id");
if($stmt == FALSE)
{
die($this->conn->error);
}
else
{
$stmt->execute();
$stmt->bind_result($user_id, $email, $name, $phone, $picture, $status);
while($stmt->fetch())
{
$user->setUserId($user_id);
$user->setEmail($email);
$user->setName($name);
$user->setPhone($phone);
$user->setPicture($picture);
$user->setStatus($status);
}
$stmt->close();
return $user;
}
}
and this is my code in index.php who called the object method.
$app->get('/users', 'authenticate', function()
{
global $user_id;
$response = array();
$user = new User();
// fetching all users
$userDB = new UserDb(MySqlDb::getInstance()->connect());
$result = $userDB->getAllUsers($user);
$response["error"] = false;
$response["users"] = array();
// looping through result and preparing users array
while ($user = $result->fetch_assoc())
{
$tmp = array();
$tmp["user_id"] = $user->getUserId();
$tmp["email"] = $user->getEmail();
$tmp["name"] = $user->getName();
$tmp["phone"] = $user->getPhone();
$tmp["picture"] = $user->getPicture();
$tmp["status"] = $user->getStatus();
array_push($response["users"], $tmp);
}
echoRespnse(200, $response);
});
please check my foreach, is it wrong ?
Thanks :)
getAllUsers returns an array of Users
so $result is an array of Users, not a database query result as you may expect.
then you call fetch_assoc() on $result, which is an array of users, and as it doesn't have this method, you get Call to undefined method User::fetch_assoc()
Related
Currently, I create an apps with a login function. To connect from android to MySQL database, I use PHP. When I use MySQLi, everything is okay. But when I convert to PDO, The error will appear the same as my question's title. Can anyone knows what is the problem? Below is my PHP code:
<?php
require_once 'configPDO.php';
$response = array();
if(isTheseParametersAvailable(array('badgeid', 'pwd'))){
$badgeid = $_POST['badgeid'];
$pwd = $_POST['pwd'];
$stmt = $conn->prepare("SELECT badgeid, email, fullname, roles_id, team_id FROM users WHERE badgeid = :badgeid AND pwd = :pwd AND roles_id = 3");
// $stmt->bind_param("ss",$badgeid, $pwd);
$stmt->bindParam(':badgeid',$badgeid,PDO::PARAM_STR);
$stmt->bindParam(':pwd',$pwd,PDO::PARAM_STR);
$stmt->execute();
//$stmt->store_result();
if($stmt->rowCount() > 0){
$stmt->bindParam($badgeid, $email, $fullname, $roles_id, $team_id);
$stmt->fetch();
$user = array(
'badgeid'=>$badgeid,
'email'=>$email,
'fullname'=>$fullname,
'roles_id'=>$roles_id,
'team_id'=>$team_id
);
$response['error'] = false;
$response['message'] = 'Login successfull';
$response['user'] = $user;
}else{
$response['error'] = false;
$response['message'] = 'Invalid username or password';
}
}
echo json_encode($response);
function isTheseParametersAvailable($params){
foreach($params as $param){
if(!isset($_POST[$param])){
return false;
}
}
return true;
}
Your second bindParam() (You should read and understand what exactly this method do!) inside the if condition is nonsense!
Change this:
if($stmt->rowCount() > 0){
$stmt->bindParam($badgeid, $email, $fullname, $roles_id, $team_id);
$stmt->fetch();
$user = array(
'badgeid'=>$badgeid,
'email'=>$email,
'fullname'=>$fullname,
'roles_id'=>$roles_id,
'team_id'=>$team_id
);
to this:
$result = $stmt->fetch(\PDO::FETCH_ASSOC); // Get results as array
if ($result) {
// Since we only get the fields we want to send back, you can assign `$result` directly to `$response['user']`
$response['user'] = $result;
PHP had thrown an related error, which you would have seen in the raw response of you request!
So basically, I have a function getPayments(). This function should execute a query, selecting from multiple tables (with joined). Here is my code
function getPayments($userid, $schoolyear) {
$stmt = $this->con->prepare("SELECT tbl_payment.payment_receipt_type AS RType, tbl_payment.payment_receipt_number AS RNumber, tbl_feetype.feetype_name AS FName, tbl_payment.payment_amount AS PAmount, tbl_month.month_date AS MDate, tbl_payment.payment_dateadded AS PAdded
FROM tbl_payment
INNER JOIN tbl_student ON tbl_student.student_id = tbl_payment.student_id
INNER JOIN tbl_schoolyear ON tbl_schoolyear.schoolyear_id = tbl_payment.schoolyear_id
INNER JOIN tbl_feetype ON tbl_feetype.feetype_id = tbl_payment.feetype_id
INNER JOIN tbl_month ON tbl_month.month_id = tbl_payment.month_id
WHERE tbl_payment.schoolyear_id = ? AND tbl_payment.student_id = ? ORDER BY payment_dateadded DESC");
$stmt->bind_param("ss", $userid, $schoolyear);
$stmt->execute();
$stmt->bind_result($RType, $RNumber, $FName, $PAmount, $MDate, $PAdded);
$payments = array();
while ($stmt->fetch()) {
$temp = array();
$temp['paymenttype'] = $RType;
$temp['receiptnumber'] = $RNumber;
$temp['feename'] = $FName;
$temp['paymentamount'] = $PAmount;
$temp['monthname'] = $MDate;
$temp['paymentdate'] = $PAdded;
array_push($payments, $temp);
}
return $payments;
}
In my index.php file:
//getting payment details for a user
$app->get('/payment/{id}/{sy}', function (Request $request, Response $response) {
$route = $request->getAttribute('route');
// $userid = $request->getAttribute('id');
$userid = $route->getArgument('id');
$schoolyear = $route->getArgument('sy');
// $schoolyear = $request->getAttribute('sy');
$db = new DbOperation();
$payments = $db->getPayments($userid, $schoolyear);
$response->getBody()->write(json_encode(array("payments" => $payments)));
});
^ This line of code will take the returned array result from getPayment() function then encode it to json.
The problem is, after testing my API in Postman, Postman only gives me this result
{"payments":[]}
Please help me. Thank you. (Sorry for my bad english)
Find the answer.
I misplace some variables.
The line $stmt->bind_param("ss", $userid, $schoolyear); should be written as $stmt->bind_param("ss", $schoolyear, $userid); .
Everything is working now. Thank you. :)
This thread is now CLOSED.
I am getting very frustrated. I have two functions which have similar "instructions" ie: return values from a users table in the database.
The second one works fine, however the first one is returning an empty value.
Here is the code:
public function ValidateUser($username, $password)
{
$stmt = "SELECT password FROM users WHERE username = :username LIMIT 1";
if(!($grabUser = $this->db->prepare($stmt)))
{
return null;
}
$grabUser->bindParam(":username", $username, PDO::PARAM_STR);
$grabUser->execute();
$data = $grabUser->fetch();
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
echo $data['password'].'s';
if(!password_verify($password,$data['password']))
{
return null;
}
return $this->core->encrypt($data['password']);
}
I'm trying to display the $data['password'] on the page just to test whether it returns a value from the database, however it is simply returning empty, whereas the query is returning a column because it passes the
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
condition.
The $username and $password variables are both set, so they are no problem.
Just in case you ask, this is the function that does work properly:
public function ValidateFacebookUser($email, $fid)
{
$stmt = "SELECT username, password FROM users WHERE email_address = :email AND connected_fb = '1' AND connected_fb_id = :fb LIMIT 1";
if(!($grabUser = $this->db->prepare($stmt)))
{
return null;
}
$grabUser->bindParam(":email", $email, PDO::PARAM_STR);
$grabUser->bindParam(":fb", $fid, PDO::PARAM_INT);
$grabUser->execute();
$data = $grabUser->fetch();
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
return array($data['username'], $this->core->encrypt($data['password']));
}
It does turn the username and password for that case. Why does it not work in the first function?
Thanks.
No, you shouldn't mix up ->fetchColumn and ->fetch and with LIMIT 1.
What happens is that you already ->fetch() the first row. After that invocation of ->fetchColumn(), there's no more row to fetch.
public function ValidateUser($username, $password)
{
$stmt = "SELECT password FROM users WHERE username = :username LIMIT 1";
if(!($grabUser = $this->db->prepare($stmt))) {
return null;
}
$grabUser->bindParam(":username", $username, PDO::PARAM_STR);
$grabUser->execute();
$data = $grabUser->fetch(); // fetch once
// no need to add ->fetchColumn checking
$ret = null;
if(!empty($data['password']) && password_verify($password,$data['password'])) {
$ret = $this->core->encrypt($data['password']);
}
return $ret;
}
Look at the manual for fetchColumn(). You can see that this fetches the next result set. So if you've already called fetch(), there should be no next result set as per your code:
$data = $grabUser->fetch();
if(count($grabUser->fetchColumn()) <= 0)
{
return null;
}
This will always return null with a LIMIT 1 or single row result.
I have that piece of code in my php file (correctly working):
$db = new Database();
$stmt = $db->prepare("SELECT * FROM bills WHERE group_id=:gid");
$stmt->bindValue(":gid", $_SESSION['group_id'], SQLITE3_INTEGER);
if (($result = $stmt->execute()) === false) {
echo "bad";
}
$bills = $result;
while ($bill = $bills->fetchArray()) {
do stuff....
}
Then I try to put all database related code in function:
function getBillsByGID($gid) {
$db = new Database();
$stmt = $db->prepare("SELECT * FROM bills WHERE group_id=:gid");
$stmt->bindValue(":gid", $gid, SQLITE3_INTEGER);
if (($result = $stmt->execute()) === false) {
return null;
}
return $result;
}
And in the original file:
$bills = getBillsByGID($_SESSION['group_id']);
while ($bill = $bills->fetchArray()) {
do stuff...
}
That gives me message: "Warning: SQLite3Result::fetchArray(): The SQLite3Result object has not been correctly initialised in line 61" (line with while($bill = $bills->fetchArray()))
var_dump($bills) after calling the function gives object(SQLite3Result)#10 (0) { }
So how do I make a function which will work properly?
Are you closing the statement in your getBillsByGID? Calling $stmt->close() invalidates the result you got from $stmt->execute(). After that, calling $result->fetchArray() will give the error "The SQLite3Result object has not been correctly initialised in ...".
I think you need to fetch data in your function and then return your variable
function getBillsByGID($gid) {
$db = new Database();
$stmt = $db->prepare("SELECT * FROM bills WHERE group_id=:gid");
$stmt->bindValue(":gid", $gid, SQLITE3_INTEGER);
if ($stmt->execute() === false) {
return null;
}
$array = array();
while($data = $stmt->fetchArray())
{
$array[] = $data;
}
return $array;
}
Now assign the function to a variable and loop throw to get your array's data
$bills = getBillsByGID($_SESSION['group_id']);
foreach($bills as $data) {
//do stuff with $data
}
I have this PHP function :
function userParent($Username)
{
global $con;
$Result = mysqli_query($con, "SELECT Username FROM Family WHERE Parent = '$Username' LIMIT 10");
$row = mysqli_fetch_assoc($Result);
return $row;
}
that function should give me 10 rows in array, but why I just have a value in array? I tried to test that codes outside function bracket and try to add WHILE loop like this :
while ($row = mysqli_fetch_assoc($Result)){
print_r($row);
}
and it works. I got my 10 rows in array format. but it prints the result to the screen. how to make it as variable so it can be returned in function?
thanks.
UPDATE : according to Phil's answer, now here's my complete code :
<?php
function userParent(mysqli $con, $username) {
$stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
$stmt->bind_param('s', $username);
$stmt->execute();
$res = $stmt->get_result();
return $res->fetch_all(MYSQLI_ASSOC);
}
$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$mysqli = new mysqli($DbServer, $DbUser, $DbPassword, $DbName);
$arrayParent = userParent($mysqli, 'root');
print_r($arrayParent);
?>
but I got this error message :
Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/myhome/public_html/test.php on line 6
Use return:
function userParent(mysqli &$dbms, $username){
// You need to "escape" strings, which you would use in direct queries.
// OR BETTER: use mysqli prepared statements with parameter binding.
$username = mysqli_real_escape_string($dbms, $username);
$result = mysqli_query($dbms, "SELECT Username FROM Family WHERE Parent = '$username' LIMIT 10");
// Create temporary array for resultset:
$buffer = array();
// Fetch data to temporary buffer:
while ($row = mysqli_fetch_assoc($result)){
$buffer[] = $row;
}
// Free result set:
$result->free();
// Return buffer to global scope:
return $buffer;
}
$users = userParent($con, 'John');
var_dump($users);
Try mysqli_result::fetch_all instead
function userParent(mysqli $con, $username) {
$stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
if ($stmt === false) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('s', $username);
$stmt->execute();
$res = $stmt->get_result();
return $res->fetch_all(MYSQLI_ASSOC);
}
Then call it like this
$parents = userParent($mysqli, 'some username');
Read these in case you're not aware of prepared statements and parameter binding
http://www.php.net/manual/en/mysqli.prepare.php
http://www.php.net/manual/en/mysqli-stmt.bind-param.php
Update
Apparently (undocumented), the mysqli_stmt::get_result() method is only available when using the mysqlnd driver. If you cannot use this driver, try this alternative
function userParent(mysqli $con, $username) {
$stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
if ($stmt === false) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('s', $username);
$stmt->execute();
$parent = null;
$parents = array();
$stmt->bind_result($parent);
while($stmt->fetch()) {
$parents[] = $parent;
}
return $parents;
}