Hi all,
I am trying to output data from a MySQL database from the column, 'category', however what I have at the moment isnt working. Any suggestions please?

$catrow = mysql_query("SELECT category FROM tuck");

$cate=array();
while($category = mysql_fetch_assoc($catrow));{
	$cate[]=$category;
}
echo $cate[0];
echo $cate[1];

Thanks

Hi,

On first thoughts, you're fetching an associative array. This means the array is indexed by column name.

Hence you could try:

$sql = mysql_query('select category from tuck');
$categories = array();

while($row = mysql_fetch_assoc($sql)) {
    $categories[] = $row['category'];
}

echo $categories[0];
echo $categories[1];

R.

thanks for your reply however that doesnt echo anything, i also tried 'mysql_fetch_array' incase i had got that wrong but still nothing *-)

what im trying to do is grab each category from the database so that i can then have a categorised order of the items in each section.
grabbing the array instead of manually writing it will also cut down on the re-coding i have to do if they change.

Hi,

In that case, try the following instead:

$sql = 'select category from tuck';
$result = mysql_query($sql);

if(mysql_num_rows($result) > 0) {
    $categories = mysql_fetch_array($result, MYSQL_NUM);
} else {
    echo 'No categories found';
}

I'm convinced that the above code and what I posted previously should work. Have you checked that the MySQL query actually returns results on the command line or using something like phpMyAdmin?

R.

i think the echo's should be inside the while loop for it to work

thanks blocblue, thats first code did work i had a syntax error somewhere.
guess thats what you get trying to code when youre half asleep

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.