Jump to content

[SOLVED] fetch_assoc/fetch_array


thomashw

Recommended Posts

$id = $_GET['manufacturer_id'];
$prev = mysql_query("SELECT * FROM product_prev WHERE manufacturer_id=$id");
if ($prev || mysql_num_rows($prev) > 0); {
$col = @mysql_fetch_assoc($prev); {
echo "<a href=" . $col["url"] . "><img src=" . $col["image"] . " border=\"0\"></a>";
echo "<a href=" . $col["url"] . ">{$col[name]}</a>";
echo $col['desc'];

 

In the above code, the rows have a manufacturer_id which is assigned to the $id variable. manufacturer_id can be the same among the different rows in the product_prev table (depending on the manufacturer.)

 

I'm trying to get the above code to keep looping until there are no more rows in the product_prev table with the manufacturer_id of $id.

 

Can anyone help?

 

Also, can someone explain the differences between mysql_fetch_array and mysql_fetch_assoc?

 

Thanks!

Link to comment
Share on other sites

Also, can someone explain the differences between mysql_fetch_array and mysql_fetch_assoc?

 

From the manual:

mysql_fetch_array — Fetch a result row as an associative array' date=' a numeric array, or both[/quote']

mysql_fetch_assoc — Fetch a result row as an associative array
Link to comment
Share on other sites

Ohh, so with mysql_fetch_array you can use either the numeric ID key or the actual row name, while with mysql_fetch_assoc only the row name will work?

Yes. You can specify how it will work by passing either MYSQL_BOTH (default), MYSQL_ASSOC or MYSQL_NUM as the second argument.

 

$id = $_GET['manufacturer_id'];
$prev = mysql_query("SELECT * FROM product_prev WHERE manufacturer_id=$id");
if ($prev || mysql_num_rows($prev) > 0); {
$col = @mysql_fetch_assoc($prev); {
echo "<a href=" . $col["url"] . "><img src=" . $col["image"] . " border=\"0\"></a>";
echo "<a href=" . $col["url"] . ">{$col[name]}</a>";
echo $col['desc'];

 

In the above code, the rows have a manufacturer_id which is assigned to the $id variable. manufacturer_id can be the same among the different rows in the product_prev table (depending on the manufacturer.)

 

I'm trying to get the above code to keep looping until there are no more rows in the product_prev table with the manufacturer_id of $id.

 

How about this?

 

<?php
$id = intval($_GET['manufacturer_id']); // i supposed it was an integer

$prev = mysql_query("SELECT * FROM product_prev WHERE manufacturer_id={$id}");
if(mysql_num_rows($prev) > 0)
{
while($col = mysql_fetch_assoc($prev))
{
	echo "<a href=" . $col["url"] . "><img src=" . $col["image"] . " border=\"0\"></a>";
	echo "<a href=" . $col["url"] . ">{$col['name']}</a>";
	echo $col['desc'];
}
}
else {
echo "No rows with manufacturer ID {$id}";
}
?>

Link to comment
Share on other sites

That works perfectly, thank you!!

 

I've got another quick question - when I put while ($col = @mysql_fetch_assoc($prev)); in my above code (instead of just $col = @mysql_fetch_assoc($prev)), the code no longer called the field names (url, image, name, etc.) It just left the area blank. Can you tell me why that happened?

Link to comment
Share on other sites

I've got another quick question - when I put while ($col = @mysql_fetch_assoc($prev)); in my above code (instead of just $col = @mysql_fetch_assoc($prev)), the code no longer called the field names (url, image, name, etc.) It just left the area blank. Can you tell me why that happened?

 

Well, if you do while(something); then it will run for as long as something evaluates to true, but as you have no code block it won't do anything.

Link to comment
Share on other sites

Code blocks are chunks of code contained within curly braces ( { } ). Example:

if(something)
{
// here is a code block
}

while(something_else)
{
// another code block
}

The code inside the first code block is executed if something evaluates to true. The code within the second code block is executed as long as something_else evaluates to true. If the value never changes you'll have an infinite loop, i.e. the script will eventually time out and result in an error.

 

Does the exact code I provided work or do you have problems with that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.