Jump to content

Correct way to get mysql results into an array in a class


littlened

Recommended Posts

I'm using a class to get a list of holidays from a mysql database, I've never used a class before so this is why I'm abit stuck.

I just want to know what is the correct way is to get data from a mysql database, and then return the array of results so that I can access the results using something like this;

The get_offers() function below does really work well, it returns an array but the array headers are in numbers rather than the field names of the mysql database.

[code]class offers {
var $username = "??????";
var $password = "??????";
var $database = "??????";
var $hostname = "??????";

var $table = "Offers";

function offers() {
$this->dblink = mysql_connect($this->hostname,$this->username,$this->password);
mysql_select_db($this->database, $this->dblink);
}

function get_offers($airport) {
    $contents = array();
$query = "SELECT OfferID,OfferResort FROM ".$this->table." WHERE OfferDeptAir = '$airport'";
      $result = mysql_query($query, $this->dblink);
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
array_push($contents, $row);
}

return $contents;
}

}

$list_of_holidays = new classname();
$list_of_holidays->offers();

foreach ($list_of_holidays as $holiday_list) {
echo $holiday_list->HolidayLocation."<br>";
}[/code]
[quote author=bqallover link=topic=123799.msg512139#msg512139 date=1169632344]
Have a look at using [url=http://uk.php.net/manual/en/function.mysql-fetch-assoc.php]mysql_fetch_assoc[/url] instead of mysql_fetch_array.
[/quote]

No need, mysql_fetch_array() returns both numeric and associative arrays.  Just change the constant...

Change this code: [code=php:0]while ($row = mysql_fetch_array($result)){[/code]

To this: [code=php:0]while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){[/code]

Regards
Huggie

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.