thomashw Posted January 7, 2008 Share Posted January 7, 2008 $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! Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/ Share on other sites More sharing options...
Daniel0 Posted January 7, 2008 Share Posted January 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432699 Share on other sites More sharing options...
thomashw Posted January 7, 2008 Author Share Posted January 7, 2008 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? Also, if anyone can help me with my question that would be awesome. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432717 Share on other sites More sharing options...
Daniel0 Posted January 7, 2008 Share Posted January 7, 2008 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}"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432726 Share on other sites More sharing options...
thomashw Posted January 7, 2008 Author Share Posted January 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432746 Share on other sites More sharing options...
Daniel0 Posted January 7, 2008 Share Posted January 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432767 Share on other sites More sharing options...
thomashw Posted January 7, 2008 Author Share Posted January 7, 2008 Sorry... what do you mean I have no code block? Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432785 Share on other sites More sharing options...
Daniel0 Posted January 7, 2008 Share Posted January 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432814 Share on other sites More sharing options...
thomashw Posted January 7, 2008 Author Share Posted January 7, 2008 Nope, the code works perfectly. Thank you for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/84882-solved-fetch_assocfetch_array/#findComment-432816 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.