call to a member function execute() on a non-object - php

My script containing that error is this:
$stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type IN ('.$in_list.')');
$stmt->execute();
$stmt->bind_result($libelle,$activite,$adresse,$tel,$lat,$lng);
The php version running on the server (not localhost) is 5.2.17

$stmt is supposed to be an object with the method execute().
Seems like $this->db->prepare() is not returning the good result.
If $this->db is a mysqli() object you should bind the parameters like that:
if ($stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type IN (?)')) {
$stmt->bind_param("s", $in_list);
$stmt->execute();
// ...
}

Check the sql you executed,
$this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type IN ('.$in_list.')');
does not return a valid statement object.

Swap the statement bind and execute and replace result with param, It will work fine
$stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements
where type IN ('.$in_list.')');
$stmt->bind_param($libelle,$activite,$adresse,$tel,$lat,$lng);
$stmt->execute();

Your db object is null. Check if you close the connection too early or somehow you override it to null.

Related

Beginner query in php database

I have a database with the following columns: id, name, zone.
I need the function to return the name of the record that contains the zone that arrives as a parameter
static public function mdlShowName($table, $zone){
$stmt = Conection::conect()->prepare("SELECT name FROM $table WHERE zone = :$zone");
$stmt -> bindParam(":name", name, PDO::PARAM_STR);
$stmt -> execute();
return $stmt -> fetch();
$stmt-> close();
$stmt = null;
}
The parameter placeholder is not a variable. Don't use $zone, just give it a label.
$stmt = Conection::conect()->prepare("SELECT name FROM $table WHERE zone = :zone");
The name by which you bind the parameter must be the same as the label you used as a placeholder in the query. Then bind it to the PHP variable that has the value.
Don't bother with PDO::PARAM_STR or other param types. The MySQL PDO driver ignores these anyway. They might be more important if you use some other brand of RDBMS (Oracle, Microsoft, etc.).
You don't need the : in the parameter name here.
$stmt -> bindParam("zone", $zone);
An alternative is to just pass an array to execute(). If you do this, then skip the bindParam() calls.
$stmt -> execute( ["zone" => $zone] );
Tip: This is all explained in the documentation!

MySQL prepared statements throwing error in PHP

This is the code to connect to my database. I am sure the username, password and database name are correct.
$Myconn = mysqli_connect($this->host, $this->user, $this->pass, $this->DBname);
This is code for prepare statement:
$query =$Myconn->prepare("SELECT * FROM `AD` WHERE name=?");
$query->bind_param('s', $AD_Name);
$query->execute();
$query->store_result();
$query->bind_result($id, $name, $price);
and I am sure that I sent $AD_Name correctly, as well as my query.
I used AMPPS and it was working while using my code.
My problem is that my result is always null when i print $id or $name or $price.
Ali Rasheed is right that you should use fetch() after doing a bind_result(), but there is a bigger issue here. You cannot use bind_result() with SELECT * .... It will not work properly because bind_result() will not know the order of the selected elements and thus it will not know which variable should get which value. Instead, you should revise to something like:
$query =$Myconn->prepare("SELECT id, name, price FROM `AD` WHERE name=?");
$query->bind_param('s', $AD_Name);
$query->execute();
$query->store_result();
$query->bind_result($id, $name, $price);
$query->fetch();
Substitute the column names as necessary of course.
You can see a good explanation about that here: https://stackoverflow.com/a/18753263/2694511
After doing
$query->bind_result($id, $name, $price);
use
$query->fetch();

Get a value from a prepared statment using mysqli

I'm trying to fetch only one value of returned row .. here's what i have tried
function getUserEmail($username) {
global $mysqli;
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM users WHERE username =? LIMIT 1");
$stmt->bind_param("s", $username);
$stmt->execute();
$userEmail = $stmt->fetch_object()->useremail;
return $userEmail;
}
I get this error
Fatal error: Call to undefined method mysqli_stmt::fetch_object()
what I'm looking for is only getting the user's email no need to fetch other data.
mysqli_stmt doesn't have a method fetch_object, but mysqli_result does.
see http://docs.php.net/manual/en/mysqli-stmt.get-result.php

PHP mysqli bind_param types

Hi I have the following:
$query = "select * from test_admin_users where school_id=? and username=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("is", $school_id,$username);
$username='jones';
$school_id=11;
$stmt->execute();
Which works as expected. Being new to mysqli_ I played around with the bind_param types and found that
$stmt->bind_param("ss", $school_id,$username);
$stmt->bind_param("ii", $school_id,$username);
Both also give the expected results. Why are incorrect types being accepted?
Also, is there any way to use an identifier such as username instead of ? in the query template?
Thanks.

PDO and alphanumeric strings?

The current error when running this from the command line is "Call to a member function bindParam() on a non-object" which I've worked out to being a problem with the variable $orderPO. Something does not like non-numeric characters which led me to the bindParam PARAM_STR business which does not work either. The database fields are both varchar 50.
My search skills are failing me. I know this must be posted somewhere about a million times but I can't seem to find it. I am completely open to doing this another way if someone has a better idea.
Current attempt code:
try
{
$orderNum = '123456';
$orderPO = '123456-A';
$dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
$stm = $dbh->prepare("insert into some_table (order_number, order_po)");
$stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);
$stm->execute();
print_r($stm);
print_r($dbh);
$arr = $stm->errorInfo();
print_r($arr);
$stm->closeCursor();
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
In order to bind parameters using PDO, you will need to use placeholders, like this:
$stm = $dbh->prepare("
INSERT INTO `some_table` SET
`order_number` = :order_number,
`order_po` = :order_po
");
$stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);
Notice the inclusion of the : character before the named placeholder. I also added column names to your query.
Read further and see examples: PDO bindParam
The correct syntax is
$stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (?, ?)");
$stm->bindParam(1,$orderNum);
$stm->bindParam(2,$orderPO);
include the questions marks, the numbers in the bindParam call refer to which question mark you're binding the parameter to
You are trying to use bindparam, but bind param matches ? not cursors :. You have not included any parameters or values.
Also, you are missing your VALUES statement within the query, which is causing the query to fail. This is why you get the "Call to a member function bindParam() on a non-object"
To use the :value syntax, use bindValue, not bindParam. to use bindParam, switch the :value to ? in your query and number them in order is your execute array.
try
{
$orderNum = '123456';
$orderPO = '123456-A';
$dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
$stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (:order_number, :order_po)");
$stm->bindvalue(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindvalue(':order_po', $orderPO, PDO::PARAM_STR);
$stm->execute();
print_r($stm);
print_r($dbh);
$arr = $stm->errorInfo();
print_r($arr);
$stm->closeCursor();
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

Categories