I am having a time tryng to pull ALL records out of a database. For example I have the following
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 50") or die(mysql_error());
// Variables to pull from the database
// I know the line below is the culprit now, so i must change the code below correct? ///
$returneddata = mysql_fetch_array($result);
///////////////////////////////////////////
$LatinName = $returneddata['Latin_Name'];
$CommonName = $returneddata['Common_Name'];
$Category = $returneddata['Category'];
$Type = $returneddata['Type'];
$Fruit = $returneddata['Fruit'];
$Flower = $returneddata['Flower'];
$MinHeight = $returneddata['Min_Height'];
$MaxHeight = $returneddata['Max_Height'];
$MinWidth = $returneddata['Min_Width'];
$MaxWidth = $returneddata['Max_Width'];
$Exposure = $returneddata['Exposure'];
$Comments = $returneddata['Comments'];
$SoilType = $returneddata['Soil_Type'];
$Zone = $returneddata['Zone'];
$PotSize = $returneddata['Pot_Size'];
$CostPrice = $returneddata['Cost_Price'];
$RetailPrice = $returneddata['Retail_Price'];
$ImageName = $returneddata['Image_Name'];
$ImageNameThumb = $returneddata['Image_Name_Thumb'];
$num_rows = mysql_num_rows($result); echo "$num_rows Rows\n";
while ($row = mysql_fetch_array($result)) {
echo " <tr>
<td align=\"center\" bgcolor=\"#000000\">
<p><img src=\"$row[Image_Name]\" style=\"width:120px;height:auto;\"></p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Latin_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Common_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Category]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Type]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Flower]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Comments]</p>
</td>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p><a href=\"editplant.php?get=$row[id]\">Edit</a></p>
<p><a href =\"print_sign_div.php?get=$row[id]\">Print</a></p>
</td>
</tr>
";
}
The issue i have is that it will pull all the records except for the first one. Every other record shows, there are only 5 records currently.
I am missing something silly I know it. I looked through questions and could not find the answer. Thanks
$count = mysql_num_rows($result); print "Count is $count";
Before you do any of your looping to verify mysql is grabbing the number of rows you're expecting. If you can update the above with your full code you're now using (as well as a dump of "SELECT * from plant_info", that would help people be able to help yo - DaOgre 2012-04-05 23:13
$Variable = $returneddata['column_name'];
In this page. sorry guys, but thanks for all the help. need sleep much - ATechGuy 2012-04-05 23:27
It could be because you're calling the mysql_fetch_array
command twice.
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
You only need to declare mysql_fetch_array
once, which is inside the while
parameter.
This should be your code:
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30");
while ($row = mysql_fetch_array($result)) {
echo " <tr>
<td align=\"center\" bgcolor=\"#000000\">
<p><img src=\"$row[Image_Name]\" style=\"width:120px;height:auto;\"></p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Latin_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Common_Name]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Category]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Type]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Flower]</p>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p>$row[Comments]</p>
</td>
</td>
<td align=\"center\" bgcolor=\"#90c084\">
<p><a href=\"editplant.php?get=$row[id]\">Edit</a></p>
<p><a href =\"print_sign_div.php?get=$row[id]\">Print</a></p>
</td>
</tr>
";
}
$numrows = mysqlnumrows($result); echo "$numrows Rows\n";
Gives me "5 - ATechGuy 2012-04-05 23:06
you didn't post valid/all your PHP - there's no mysql_query()
call.
SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
line 2 there fetches a row and does nothing with it anyway, throwing away the first row. try this:
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30");
while ($row = mysql_fetch_array($result)) {
After your question update, it looks like you want to pull out info from the first row, then loop over all the rows. Here's a way to do that:
$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30");
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
$row = $rows[0];
$LatinName = $returneddata['Latin_Name'];
...
foreach ($rows as $row){
...
}
$returneddata = mysqlfetcharray($result);
Which was taking my first result, but i use this to display data like so:
$LatinName = $returneddata['Latin_Name'];
hmmm - ATechGuy 2012-04-05 23:11