这是我试图用来在一个表中显示来自多个MySQL表的信息的代码。如果表格信息不存在,我也会尝试创建一个else子句。这是我失败的尝试。
我一直在编辑它,因为我啄它,但此时我仍然无法在行不存在时显示最终的其他内容。
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td><a href=\"edit_item.php?id=$row[ItemID]\" target=\"_blank\">Edit</a></td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td><a href=\"photo_upload.php?id=$row[ItemID]\" target=\"_blank\">Pics</a></td>");
echo ("<td><a href=\"item_info.php?id=$row[ItemID]\" target=\"_blank\">Info</a></td></tr>");
$info = "SELECT * FROM Info WHERE Item_ItemID = $row[ItemID] ";
$info_results = mysql_query($info);
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\"><a href=\"item_info.php?id=$row[ItemID]\" target=\"_blank\">Add Extra Information</a></td></tr>");
}
}
}
?>
答案 0 :(得分:1)
您可以将$ info "SELECT * FROM Info"
中的SQL语句重新编写为"SELECT * FROM Info wHERE Item_ItemID=$row['ItemID']
...然后删除
if ($info_row['Item_ItemID'] = $row['ItemID'])
再加上您的新代码after $info_results = mysql_query($info);
*已编辑*
然后你喜欢这个,而不是
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\"><a href=\"item_info.php?id=$row[ItemID]\" target=\"_blank\">Add Extra Information</a></td></tr>");
}
}
试试这个
if(mysql_num_rows($info)>0){
while($info_row = mysql_fetch_array($info){
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
}else {
echo ("<tr><td colspan=\"12\"><a href=\"item_info.php?id=$row[ItemID]\" target=\"_blank\">Add Extra Information</a></td></tr>");
}
答案 1 :(得分:0)
我尝试编辑您的代码,但我无法尝试。
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td><a href=\"edit_item.php?id=$row[ItemID]\" target=\"_blank\">Edit</a></td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td><a href=\"photo_upload.php?id=$row[ItemID]\" target=\"_blank\">Pics</a></td>");
echo ("<td><a href=\"item_info.php?id=$row[ItemID]\" target=\"_blank\">Info</a></td></tr>");
$info = "SELECT * FROM Info where Item_ItemID = ".$row['ItemID'];
$info_results = mysql_query($info);
if ($info_row = mysql_fetch_array($info_results))
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
else { //else dispaly Add information to info link.
echo ("<tr><td colspan=\"12\"><a href=\"item_info.php?id=$row[ItemID]\" target=\"_blank\">Add Extra Information</a></td></tr>");
}
}
?>
我希望这就是答案。