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

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?

Related

Catchable fatal error: Object of class PDOStatement could not be converted to string pdo php sql [duplicate]

This question already has answers here:
Catchable fatal error: Object of class PDOStatement could not be converted to string in D
(2 answers)
Closed 6 years ago.
I know this question has already been answered but i doesn't realy get the solution of my problem. I actualy try to select the numbers of salaries to a sql base but i have this error : "Catchable fatal error: Object of class PDOStatement could not be converted to string"
Here is my code:
function getNbSalaries(){
global $pdo;
$query = "SELECT count(*) as nb FROM salaries ;";
$prep= $pdo->prepare($query);
$prep->fetch(PDO::FETCH_ASSOC);
return $prep;
}
You have to use either
->prepare()
->execute()
or
->query()
In this case as it is a query without any parameters this may well be the simplest solution
function getNbSalaries(){
global $pdo;
$query = "SELECT count(*) as nb FROM salaries";
$prep= $pdo->query($query);
$row = $prep->fetch(PDO::FETCH_ASSOC);
return $row;
}
It is not a good idea to use global like this, it would be a better idea to pass the connection as a parameter of the function like this
function getNbSalaries($pdo){
$query = "SELECT count(*) as nb FROM salaries";
$prep= $pdo->query($query);
$row = $prep->fetch(PDO::FETCH_ASSOC);
return $row;
}
$row = getNbSalaries($pdo);
echo $row['nb']'
that don't work but i see where is the problem i think:
my function :
function getNbSalaries($pdo){
$query = "SELECT count(*) as nb FROM salaries ;";
$prep= $pdo->prepare($query);
$prep->fetch(PDO::FETCH_ASSOC);
$prep->execute();
return $prep;
}
and there is my code to return the result on my main page:
<p>Nombre de salariƩs : <?= getNbSalaries(); ?> </p>

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

Laravel 5:Call to a member function fetch_assoc() on a non-object [duplicate]

This question already has answers here:
Fatal error: Call to a member function fetch_assoc() on a non-object [duplicate]
(4 answers)
Closed 7 years ago.
<?php $results = DB::select('select * from insurance_policy where Id = ?', [1]);
$row = $results->fetch_assoc();
echo $row["Amount"];
?>
Here is my code it is giving me a error:Call to a member function fetch_assoc() on a non-object.
I am using Laravel 5
DB::select would return an Array which stores the DB rows.
You can iterate the results
foreach ($results as $row) { echo $row['Amount']; }

Error: Call to undefined method mysqli_stmt::fetch_object() [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()

Fatal error: Call to a member function setFetchMode() on a non-object [duplicate]

This question already has an answer here:
PDO Function setFetchMode
(1 answer)
Closed 9 years ago.
I am getting this error in PHP:
Fatal error: Call to a member function setFetchMode() on a non-object
Source code:
<?php
$c1=103;//customer ID
$c2=104;//customer ID
try {
$conn = new PDO("mysql:host=localhost;dbname=stored_procedure_examples","root","");
// execute the stored procedure
$sql = 'CALL GetCredit($c1,$c2)';//passing customer ID 103 and 104 as parameter
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table>
<tr>
<th>Credit Limit</th>
</tr>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['creditlimit'] ?></td>
</tr>
<?php endwhile; ?>
</table>
MySQL Stored Procedure:
DELIMITER $$
CREATE PROCEDURE GetCredits(in p_cusno1 int(11),p_cusno2 int(11))
BEGIN
SELECT creditlimit FROM customers WHERE customerNumber IN(p_cusno1,p_cusno2);
END$$
Note: if I pass the customer ID directly it works
if I pass indirectly by using variables like c1 and c2 it's throwing error
Fatal error: Call to a member function setFetchMode() on a non-object
pass the variables through double quotes, because single quotes don't get the value of variables.
change:
$sql = 'CALL GetCredit($c1,$c2)';
to:
$sql = "CALL GetCredit($c1,$c2)";

Categories