Jump to content

[SOLVED] general question about pdo


devdavad

Recommended Posts

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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.