Mysql / PHP代码返回除first和last之外的所有行

时间:2012-04-05 22:53:26

标签: php mysql arrays

我正在尝试从数据库中提取所有记录。例如,我有以下

$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>

";
}

我遇到的问题是它将拉除除第一个以外的所有记录。其他记录显示,目前只有5条记录。

我错过了一些我知道的傻事。我查看了问题并找不到答案。感谢

2 个答案:

答案 0 :(得分:3)

可能是因为您正在调用mysql_fetch_array命令两次。

$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {

您只需要在mysql_fetch_array参数内声明while一次。

这应该是你的代码:

$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>

";
}

答案 1 :(得分:1)

您没有发布有效/所有PHP - 没有mysql_query()来电。

SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {

第2行取出一行并且无论如何都不做任何事情,丢掉第一行。试试这个:

$result = mysql_query("SELECT * FROM `plant_info` ORDER BY id LIMIT 0, 30");
while ($row = mysql_fetch_array($result)) {

更新问题后,看起来您想要从第一行提取信息,然后遍历所有行。这是一种方法:

$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){
    ...
}