0

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

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by ben.marks
0

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.

0

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.

0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.