Jump to content

[SOLVED] PDO (what's wrong with this code)


sKunKbad

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

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.