littlened Posted January 24, 2007 Share Posted January 24, 2007 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 Link to comment https://forums.phpfreaks.com/topic/35491-correct-way-to-get-mysql-results-into-an-array-in-a-class/ Share on other sites More sharing options...
bqallover Posted January 24, 2007 Share Posted January 24, 2007 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 Link to comment https://forums.phpfreaks.com/topic/35491-correct-way-to-get-mysql-results-into-an-array-in-a-class/#findComment-167926 Share on other sites More sharing options...
HuggieBear Posted January 24, 2007 Share Posted January 24, 2007 [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]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/35491-correct-way-to-get-mysql-results-into-an-array-in-a-class/#findComment-167941 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.