devdavad Posted November 28, 2008 Share Posted November 28, 2008 Alright I'm trying to execute this sql query (with postgresql of course) "SELECT event FROM calendar WHERE day = " . $day; with this first example I got the information ok and everything include("connection.php"); $sql = "SELECT event FROM calendar WHERE day = 27"; foreach($conn->query($sql) as $row) { print $row["event"]; } $conn = null; but for some reason when that is re-written as a function for use in another script I get this error: Fatal error: Call to a member function query() on a non-object function getevents($day) { $sql = "SELECT event FROM calendar WHERE day = " . $day; foreach($conn->query($sql) as $event) { return $event["event"]; } } by the way both scripts include connection.php which has the connection information Quote Link to comment https://forums.phpfreaks.com/topic/134602-solved-general-question-about-pdo/ Share on other sites More sharing options...
devdavad Posted November 28, 2008 Author Share Posted November 28, 2008 GOD DAMN IT HOW DOES MY SQL SERVER PASSWORD ALWAYS END UP IN MY POSTS I can't believe that happened a second time... http://www.phpfreaks.com/forums/index.php/topic,204927.msg929100.html#msg929100 Quote Link to comment https://forums.phpfreaks.com/topic/134602-solved-general-question-about-pdo/#findComment-700831 Share on other sites More sharing options...
btherl Posted November 28, 2008 Share Posted November 28, 2008 You need to build a result set and return it all at once, instead of having a return inside the foreach loop. You also need to bring $conn into scope. Try this: function getevents($day) { global $conn; $sql = "SELECT event FROM calendar WHERE day = " . $day; $results = array(); foreach($conn->query($sql) as $event) { $results[] = $event["event"]; } return $results; } Quote Link to comment https://forums.phpfreaks.com/topic/134602-solved-general-question-about-pdo/#findComment-700839 Share on other sites More sharing options...
devdavad Posted November 28, 2008 Author Share Posted November 28, 2008 thank you so much, topic solved actually all I had to do on the function getevents() was add the line global $conn; anyway thank you again very much, my calendar app is now complete! Quote Link to comment https://forums.phpfreaks.com/topic/134602-solved-general-question-about-pdo/#findComment-700843 Share on other sites More sharing options...
btherl Posted November 30, 2008 Share Posted November 30, 2008 You're welcome Did you test it with multiple events on a day? The second code fragment in your first post will only work with a single event. Quote Link to comment https://forums.phpfreaks.com/topic/134602-solved-general-question-about-pdo/#findComment-702088 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.