I've been making some simple queries to a mysql table from within a PHP script.
Everything works as expected the first time I loop and use the mysql_fetch_array function. Once I break out of the loop and start a new loop, the function no longer appears to be iterating from where it left off.. or at all.. It's not like I just end up getting the wrong row back from the loop, Suddenly my variable $row seems to have nothing assigned to it at all.
<?php
// Connecting, selecting database
$link = mysql_connect('mysql.domain.com', 'user', 'password')
or die('Could not connect: ' . mysql_error());
mysql_select_db('db_trial') or die('Could not select database');
// Grab a subset of rows that match an expression. This works.
$query = "SELECT * FROM products WHERE item REGEXP '^($collection)[0-9]'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Iterate through the subset of rows until some condition is met... This works.
$i=0;
do {
$row = mysql_fetch_array($result, MYSQL_NUM);
$i++;
} while ($i != 4 * ($page - 1) + 1);
//Now that we have the correct row, use its values to create some variables.. This works
$p = 0;
$product_thumb[$p] = './images/'.$row[0].'.jpg'; // echo output is "./images/xxy32.jpg"
$product_name[$p] = $row[1]; // echo output is "WEED WHACKER"
//Now I want to continue iterating through the subset and create some more variables. This does not work!!
while ($row = mysql_fetch_array($result, MYSQL_NUM) && $p<3) {
$p++;
$product_thumb[$p] = './images/'.$row[0].'.jpg';
$product_name[$p] = $row[1];
echo $product_thumb[$p]; // output is "./images/.jpg"
echo $product_name[$p]; //nothing is output
}
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
Can anyone explain what is going on here?