This question already has answers here:
Call to undefined method mysqli_result::fetch()
(2 answers)
Closed 1 year ago.
in this code i get the error: Fatal error: Call to undefined method mysqli_stmt::fetch_array()
What is the problem?
$search= "player";
($sql = $db->prepare('select job from jobs where job like ?'));
$sql->bind_param('s', $search);
$sql->execute();
$sql->bind_result($search);
$data = array();
while ($sql->fetch_array(MYSQLI_ASSOC)) {
$data[] = array(
'label' => $row['job']
);
echo json_encode($data);
}
$sql -> close();
$db -> close();
thanks
Using prepared statements there is no fetch_array(). Use mysqli_stmt::fetch() instead or to fetch multiple records use mysqli_result::fetch_all()
Check the manual: mysqli_stmt::fetch() or mysqli_result::fetch_all()
Related
This question already has answers here:
Fatal error: Call to a member function rowCount() on a non-object
(2 answers)
Closed 3 years ago.
try {
$sql = "SELECT * FROM tbl4 WHERE tur=:tur AND link=:link";
$res = $db -> prepare($sql);
$res -> bindParam(":tur",$tur);
$res -> bindParam(":link",$link);
$res -> execute();
$res = $res -> fetch(PDO::FETCH_ASSOC);
if($res->rowCount()!=0){
echo 'OK';
}else{
echo "No!";
}
}catch (Exception $e) {
echo "Error5";
}
But the problem is I get that error
Uncaught Error: Call to a member function rowCount() on array in C:\xampp\htdocs\sf-19\pages\topic.php:19 Stack trace: #0 C:\xampp\htdocs\sf-19\index.php(18): require() #1 {main} thrown in C:\xampp\htdocs\sf-19\pages\topic.php on line 19
If I delete if statement (has rowCount) everything works well. Yet I want to check if the query has result.
What am I missing? or what is the best way to check if query has result?
Thanks
Your problem is this line:
$res = $res -> fetch(PDO::FETCH_ASSOC);
which is overwriting the value of $res before you use it in
if($res->rowCount()!=0){
You should probably use a different variable name for that value e.g.
$rows = $res -> fetch(PDO::FETCH_ASSOC);
This question already has answers here:
Call to undefined method mysqli_stmt::get_result
(10 answers)
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
Error: Uncaught Error: Call to undefined method mysqli_stmt::get_result()
How to use it right?
public static function login($benutzername, $passwort){
$result = Connection::getInstance()->db;
$sql = 'SELECT * FROM users WHERE benutzername = ? AND passwort = ?';
$stmt = $result->prepare($sql);
$passwort = md5($passwort);
maybe use mysqli-stmt.bind-result ?
$stmt->bind_param('ss', $benutzername, $passwort);
$stmt->execute();
$result = $stmt->get_result();
or mysqli_stmt_fetch?
if ($result->num_rows != '0') {}
Thanks a lot!
This question already has answers here:
Call to undefined method mysqli_stmt::get_result
(10 answers)
Closed 5 years ago.
I am writing some code to fetch data using prepared statements. Below is my code
$statement = $connection->prepare("select * from products where id =?");
$statement->bind_param('d',$id);
$statement->execute();
$result = $statement->get_result();
$product_details = $result->fetch_all();
I am using PHP 5.6.This works fine on my localhost with no error. When I uploaded this file to my Godaddy Host this will gives error:
PHP Fatal error: Call to undefined method mysqli_stmt::get_result()
I searched over internet, but not found satisfied results, On stackoverflow I found several question similar to this but not got solution for me.
On Godddy host I enabled mysqld extension and also running PHP 5.6 So any help
There might be issue with your $statement->execute(). try to run this code in try catch block, may be you find some exception. or run this method with if block.
if($statement){
$result = $statement->get_result();
}
or
try{
$statement = $connection->prepare("select * from products where id =?");
$statement->bind_param('d',$id);
$statement->execute();
if($statement){
$result = $statement->get_result();
}
$product_details = $result->fetch_all();
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
This question already has answers here:
Call to undefined method mysqli_result::fetch()
(2 answers)
Closed 1 year ago.
in this code i get the error: Fatal error: Call to undefined method mysqli_stmt::fetch_array()
What is the problem?
$search= "player";
($sql = $db->prepare('select job from jobs where job like ?'));
$sql->bind_param('s', $search);
$sql->execute();
$sql->bind_result($search);
$data = array();
while ($sql->fetch_array(MYSQLI_ASSOC)) {
$data[] = array(
'label' => $row['job']
);
echo json_encode($data);
}
$sql -> close();
$db -> close();
thanks
Using prepared statements there is no fetch_array(). Use mysqli_stmt::fetch() instead or to fetch multiple records use mysqli_result::fetch_all()
Check the manual: mysqli_stmt::fetch() or mysqli_result::fetch_all()
This question already has answers here:
mysqli::query(): Couldn't fetch mysqli
(4 answers)
Closed 9 months ago.
I started using mysqli_* functions instead of the old mysql_* functions in PHP, and I'm having a bit of a problem with this code:
public function addUser($table, $code = false, $rows = false) {
if (!$code) {
header("Location: " . $this->authenticate());
} else {
$this->getToken($code);
}
$user = $this->getEndpoint('users/current', false, true);
$user = $user->response->user;
if (!$rows)
$rows = array(
"remote_id" => $user->id,
"firstName" => $user->first_name,
"lastName" => $user->last_name,
"photo" => $user->photo->medium,
"gender" => $user->gender == 'male' ? 1 : 2,
"email" => $user->contact->email,
);
$rows['access_token'] = $this->accessToken;
$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = '{$this->accessToken}'"); //line 136
$stmt->execute(); //line 137
}
The code returns these 2 errors:
Warning: mysqli::prepare(): Couldn't fetch MySQL in C:\Users\Grega\Server\application\inc\classes\APIConnect.php on line 136
Fatal error: Call to a member function execute() on a non-object in C:\Users\Grega\Server\application\inc\classes\APIConnect.php on line 137
What is the reason for 'Couldn't fetch MySQL'? The database connection is correct, it works in other classes, and the query returns a valid result, if I echo it and execute it in phpMyAdmin. Also, my variable is named mysql NOT mysqli!
You should read more about the difference between MYSQL to MYSQLi.
While your code is:
$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = '{$this->accessToken}'");
You should do it like this:
$stmt = $this->mysql->prepare("SELECT id FROM users WHERE access_token = ?");
$stmt->bind_param("s" , $this->accessToken ); //Used 's' as I guess that the accessToken is a string
The binding part is the critical part of the prepare thing. (Your queries are safe)
After that you can use $stmt->execute(); and get_result().
For the first error: you probably closed the connection somewhere before this code, and this is why ($stmt -> close() )
For the second error: when you use (prepare()), you first introduce a SQL template to the database, which then has to pass the parameters to the "bind_param()" method to send it to the database with another protocol (to prevent SQL injection) and reduce the request and attach the parameters to SQL with execute() method