Error: Call to undefined method mysqli_stmt::fetch_object() [duplicate] - php

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

PDO Fetch and rowCount (PHP) [duplicate]

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);

Uncaught Error: Call to undefined method mysqli_stmt::get_result() [duplicate]

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!

PHP Fatal error: Call to undefined method mysqli_stmt::get_result() on Godaddy host [duplicate]

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";
}

OOP alternative to the mysqli_stmt::fetch_array() [duplicate]

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()

Call to a member function prepare() on a non-object pdo [duplicate]

This question already has answers here:
pdo - Call to a member function prepare() on a non-object [duplicate]
(8 answers)
Closed 6 years ago.
I tried to add a section to IMPORT infos from a form into a db table and I get this error:
Fatal error: Call to a member function prepare() on a non-object in /srv/disk7/2173760/www/site.net/admin.php on line 79
and this is the code I tried to use:
<?php
$pdo = new PDO('mysql:host=;dbname=', '', '');
$sql = "SELECT * FROM games LIMIT 10";
foreach ($pdo->query($sql) as $row) {
if(isset($_POST['insert']))
{
$game_title=$_POST['gtitle'];
$yt_id=$_POST['ytlink'];
$name=$_POST['gtitle'];
$source=$_POST['slink'];
$url=$_POST['keysl'];
$steam_id=$_POST['appid'];
$categories=$_POST['inlineRadioOptions'];
$query_ins="INSERT INTO tbl_games(ytlink,gtitle,slink,keysl,appid,keysleft) VALUES(:yt_id,:name,:source,:url,:steam_id,:categories)";
$stmt_query=$dbh->prepare($query_ins);
$games_ins=$stmt_query->execute(array(":yt_id"=>$ytlink,":name"=>$gtitle,":source"=>$slink,":url"=>$keysl,":steam_id"=>$appid,":categories"=>$keysleft));
if(!$games_ins)
{
$error=$stmt_query->errorInfo();
echo $error['2'];
}
}
?>
this is line 79:
$stmt_query=$dbh->prepare($query_ins);
I replaced with this and still don't working..
<?php
$pdo = new PDO('mysql:host=;dbname=', '', '');
$sql = "SELECT * FROM games LIMIT 10";
foreach ($pdo->query($sql) as $row) {
$query = "INSERT INTO tbl_games(yt_id,name,url,source,keysleft,steam_id)".
"SELECT ytlink,gtitle,slink,keysl,appid FROM games LIMIT 10"
?>
You have to instantiate $dbh before use it, like you've done with $pdo.
Why don't you use the object $pdo from the second line instead?

Categories