Web Hosting Talk







View Full Version : PHP: Array How to Retrieve & Display from DB


mr_ash
04-30-2006, 03:52 PM
Hi,

I am in need of some serious help here. I am having problems in creating some funtions that will gather categories from my DB:

The function that I have done to do this is:

function get_categories()
{

$conn = mysql_connect('', '', '');
mysql_select_db('', $conn);

$query = "select product_category from PRODUCT_CATEGORY";

$result = @$conn->query($query);
if (!$result)
return false;
$num_cats = @$result->num_rows;
if ($num_cats ==0)
return false;
$result = db_result_to_array($result);
return $result;
}

The problem also lies where I cant get PHP to convert these into an array can anyone help me to construct a function that will put my results into an array and then display them as a link.

adaml
05-03-2006, 08:09 AM
Welcome to the WebHostingTalk! This should be in the programming discussion forum, its not a tutorial! Anyway, here goes:


function get_categories() {
$conn = mysql_connect('', '', '');
$selectDB = mysql_select_db('', $conn);


$result = @mysql_query($query);
if (!$result) {
return false;
}

if (@mysql_num_rows == 0) {
return false;
}
while ($rows = mysql_fetch_array($result)) {
$categories[] = $rows;
}

return $categories;
}


It would be a good idea to set a global connection to the database rather than setting up a new connection in each function that requires a connection. Also you shouldnt use OOP style coding when your doing everything else procedural its bad practise. And try sticking your code in PHP tags when your writing a message!

Try searching on the google for some tutorials and resources and check out www.phpfreaks.com and www.php.net!


Hope this helps!