Jump to content

Recommended Posts

Why do I get:

Fatal error: Call to a member function execute() on a non-object in /home/content/test/pdotest2.php5 on line 18

 

<?php
ini_set('display_errors', 1);
try{
	$pdo = new PDO('mysql:host=1.secureserver.net;dbname=tips', 'tips', 'ChromeDomeX');
	foreach ($pdo->query('SELECT * FROM tipsntricks WHERE tipType = "performance"') as $row) {
		echo "<p>Tip number " . $row['tipNum'] . " is:<br />";
		echo $row['tipTitle'] . "</p>";
	}

	$pdo2 = $pdo->query('SELECT * FROM tipsntricks WHERE tipType = "aerodynamics"');
	$result = $pdo2->fetch(PDO::FETCH_ASSOC);
	extract($result);
	echo "<p>Tip number " . $tipNum . " is:<br />";
	echo $tipTitle . "</p>";

	$searchType = 'endurance';
	$sql = $pdo->prepare("SELECT * FROM tipsntricks WHERE tipType = '$searchType'");
	$sql->execute();
	$numRows = count($sql->fetchAll());
	if ($numRows != 0){
		echo "Number of $searchType tips: " . $numRows;
	}else{
		echo "There are no $searchType tips, sorry.";
	}

	$pdo = null;
}
catch(PDOException $e){
echo $e->getMessage();
}
?>

 

This works fine, but I was hoping to have only one object:

<?php
ini_set('display_errors', 1);
try{
	$pdo = new PDO('mysql:host=1.secureserver.net;dbname=tips', 'tips', 'ChromeDomeX');
	foreach ($pdo->query('SELECT * FROM tipsntricks WHERE tipType = "performance"') as $row) {
		echo "<p>Tip number " . $row['tipNum'] . " is:<br />";
		echo $row['tipTitle'] . "</p>";
	}

	$pdo2 = $pdo->query('SELECT * FROM tipsntricks WHERE tipType = "aerodynamics"');
	$result = $pdo2->fetch(PDO::FETCH_ASSOC);
	extract($result);
	echo "<p>Tip number " . $tipNum . " is:<br />";
	echo $tipTitle . "</p>";

	$pdo = null;
}
catch(PDOException $e){
	echo $e->getMessage();
}
?>
<?php
$pdo = new PDO('mysql:host=1.secureserver.net;dbname=tips', 'tips', 'ChromeDomeX');
$searchType = 'endurance';
$sql = $pdo->prepare("SELECT * FROM tipsntricks WHERE tipType = '$searchType'");
$sql->execute();
$numRows = count($sql->fetchAll());
if ($numRows != 0){
	echo "Number of $searchType tips: " . $numRows;
}else{
	echo "There are no $searchType tips, sorry.";
}
$pdo = null;
?>

Link to comment
https://forums.phpfreaks.com/topic/75447-solved-pdo-whats-wrong-with-this-code/
Share on other sites

Well the second set of code above works, so I was just wondering why the first doesn't. Both have the same:

$sql->execute();

but the first set of code doesn't work, and the second does. Do you see what I'm talking about?

dont know how and why loooook carefully

 

$sql = $pdo->prepare("SELECT * FROM tipsntricks WHERE tipType = '$searchType'");

$sql->execute();

 

do you thing $sql is an object with that code?

 

$sql is a value of the result for this $pdo->prepare("SELECT * FROM tipsntricks WHERE tipType = '$searchType'");

 

so $sql is not an object

 

hope see my point

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.