sKunKbad Posted October 31, 2007 Share Posted October 31, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75447-solved-pdo-whats-wrong-with-this-code/ Share on other sites More sharing options...
teng84 Posted October 31, 2007 Share Posted October 31, 2007 $sql = $pdo->prepare("SELECT * FROM tipsntricks WHERE tipType = '$searchType'"); $sql->execute();<----- $ql is an object? where did you define it Quote Link to comment https://forums.phpfreaks.com/topic/75447-solved-pdo-whats-wrong-with-this-code/#findComment-381685 Share on other sites More sharing options...
sKunKbad Posted October 31, 2007 Author Share Posted October 31, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/75447-solved-pdo-whats-wrong-with-this-code/#findComment-381687 Share on other sites More sharing options...
teng84 Posted October 31, 2007 Share Posted October 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/75447-solved-pdo-whats-wrong-with-this-code/#findComment-381689 Share on other sites More sharing options...
sKunKbad Posted October 31, 2007 Author Share Posted October 31, 2007 I see your point 100%, but that is why I am so confused. Look at the samples in the documentation: PDOStatement->execute(). I am using this PDO statement just as they are, am I not? Quote Link to comment https://forums.phpfreaks.com/topic/75447-solved-pdo-whats-wrong-with-this-code/#findComment-381691 Share on other sites More sharing options...
sKunKbad Posted October 31, 2007 Author Share Posted October 31, 2007 I figured it out. Adding: $pdo2->closeCursor(); before $sql = $pdo->prepare("SELECT * FROM tipsntricks WHERE tipType = '$searchType'"); was all that was needed. Quote Link to comment https://forums.phpfreaks.com/topic/75447-solved-pdo-whats-wrong-with-this-code/#findComment-381696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.