Call to undefined method PDO::execute()
->execute()
is for prepared statements. e.g.
$stmt = $dbh->prepare('some query here');
$stmt->execute();
You're trying to execute a query directly on the main DB object. For PDO, that means
$dbh->exec('query goes here');
You really should look into prepared statements. You're vulnerable to SQL injection attacks as is, and since you're using PDO to begin with, it's basically unforgivable to NOT be writing safe queries.
Call to undefined method PDO execute
execute
is a method of the PDOStatement
class (see docs), not of PDO
. The return value of PDO::prepare
is an instance of PDOStatement
, so replace $pdo->execute();
with $query->execute();
The bindParam
calls also seem incorrect to me, as the docs say on the first argument ($parameter
):
Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.
This means that this:
$query -> bindParam(1, $_GET['image_id'], PDO::PARAM_INT);
$query -> bindParam(1, $ip_address, PDO::PARAM_INT);
Should be:
$query->bindParam(1, $_GET['image_id'], PDO::PARAM_INT);
$query->bindParam(2, $ip_address, PDO::PARAM_INT);
Call to undefined method PDOStatement::query()
This is exactly why the consensus is that globals are evil. In your database.php
file you're setting the $db
variable to a PDO
object:
$db= new PDO($dsn, $user, $password);
And then in your categories.php file you overwrite the $db
variable with a PDOStatement
object:
$db=$db->query($query);
PDO::query()
returns a PDOStatement
object, which does not have a query()
method. Thus the error you're getting.
On another note, you need to be using prepared statements with bound parameters or else your code is vulnerable to SQL injection attacks. You can also use PDOStatement::fetchColumn()
to select a single column:
function get_category_name($category_id){
global $db; // Yuck!
$query="SELECT categoryName FROM categories
WHERE categoryID = ?";
$stmt=$db->prepare($query);
$stmt->execute([$category_id]);
return $stmt->fetchColumn();
}
Call to undefined method PDO::fetchAll()
fetchAll()
is a method of an object PDOStatement
, which is created and returned when you are calling $statement = $connect->prepare($query);
This is the fix you need to apply to your code
$statement->execute();
$result = $statement->fetchAll();
// ^--------^-----------------This instead of $connect
This query "SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'"
is vulnerable to SQL injections Consider using prepared statements.
In example :
$query = "SELECT * FROM tbl_comment WHERE parent_comment_id = :parent_id";
$statement = $connect->prepare($query);
$statement->bindParam(':parent_id', $parent_id);
$statement->execute();
Call to undefined method PDOStatement
The method is called PDOStatement->bindValue() without the trailing "s"
see http://www.php.net/manual/en/pdostatement.bindvalue.php
Fatal Error: Call to undefined method PDOStatement::prepare()
It seems, $connection
isn't what you think, it should be. As the error tolds you, $connection
is an PDOStatement
instead of PDO
and PDOStatement
hasn't a prepare
method.
This happens, because in every loop (except the first one), you overwrite $connection
with the result of prepare
(That's an PDOStatement
!)
Given, before the loop $connection
is a PDO
object, you should just change the code inside the loop to something like this
foreach ($items as $item) {
$query = $connection->prepare("INSERT INTO items (ItemName) VALUES (:ItemName)");
$query->execute(array(':ItemName' => $item));
}
Call to undefined method PDO::execute(), previous answers doesn't solve the error
You are calling execute()
from the wrong object/resource (whatever PDO is):
// this is the proper chain of calls as stated in the documentation
$statement = $conn->prepare();
$execute_result = $statement->execute();
// execute() returns a boolean so you can use an if/else block to know if your query is hunky-dory
if($execute_result)
{
// yay the query had no errors!
}
else
{
// aww snap, you messed up now!
}
http://www.php.net/manual/en/pdostatement.execute.php
Related Topics
Pass a Percent (%) Sign in a Url and Get Exact Value of It Using PHP
Login Only If User Is Active Using Laravel
Memory_Get_Peak_Usage() with "Real Usage"
How to Validate Google Recaptcha on Form Submit
PHP Form Token Usage and Handling
PHP 7 Rc3: How to Install Missing MySQL Pdo
How to Get the First Word of a Sentence in PHP
Increasing PHP Memory_Limit. at What Point Does It Become Insane
Which Compression Method to Use in PHP
Storing Datetime as Utc in PHP/Mysql
Convert Lat/Longs to X/Y Co-Ordinates
Laravel 5 - Env() Always Returns Null
How to Properly Use Bearer Tokens
Is_Null($X) VS $X === Null in PHP
How to Inject a Repository into a Service in Symfony