Jump to content

[SOLVED] OOP Mysql loop query result


papacostas

Recommended Posts

I have an OOP problem.

Im trying to create a simple DB class structure but when it lists the

result it turns in to a never ending loop, what am i doing wrong?

 

the getEvents function in Display calls the getEvents function in the DAO class

which performs the SQL query by calling the executeSQL function that returns the resultset

 

what should i change for this to work?

 

thank you

 

class Display {

function getEvents() {

include 'inc/dao.php';

$DAO=new DAO();

while ($events = $DAO->getEvents()) {

echo $events['headline'];

 

}

}

}

 

class DAO {

private $SQL;

function executeSQL($SQL) {

require_once 'inc/db.php';

$db = new DB();

$db->opendb();

$results= mysql_query($SQL);

return mysql_fetch_array($results);

}

function getEvents () {

$query = "select id, headline, startdate, desc_short, filename from events";

$query = $this->executeSQL($query);

return $query;

}

}

 

Link to comment
Share on other sites

Looks like you are simply running the same query over and over since you want the result of mysql_fetch_array in the $events, not the the method getEvents(). That will simply keep running the same query and not iterate through the result like you want. You could simply assign mysql_fetch_array($results) to a class variable and call it in place of getEvents(). That way you will have a constant value there and not a new method/SQL query each iteration...which is what you need.

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.