我有一个mysql表,其中一行要么是0还是1.但是,我希望能够用PHP显示表,并且0显示为no,1显示为yes 。我仍然是PHP的初学者,并一直在寻找一种方法来做到这一点,但没有运气。有问题的行是“杰作”行。我认为可以做到的代码行是
$row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO";
以下是显示表格的代码:
<?php
$con=mysqli_connect("localhost","username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM weapons");
$row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO";
while($row = mysqli_fetch_array($result))
{
echo "<center>";
echo "<table border='1' class='display'>";
echo "<tr>";
echo "<td>Weapon Name: </td>";
echo "<td>" . $row['weaponName'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Creator: </td>";
echo "<td>" . $row['creator'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Category: </td>";
echo "<td>" . $row['weaponCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Sub-Category: </td>";
echo "<td>" . $row['weaponSubCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Cost: </td>";
echo "<td>" . $row['costAmount'] . " " . $row['costType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(S): </td>";
echo "<td>" . $row['damageS'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(M): </td>";
echo "<td>" . $row['damageM'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Critical: </td>";
echo "<td>" . $row['critical'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Range Increment: </td>";
echo "<td>" . $row['rangeIncrement'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weight: </td>";
echo "<td>" . $row['weight'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Type: </td>";
echo "<td>" . $row['weaponType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Masterwork: </td>";
echo "<td>" . $row['masterwork'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Special Abilities: </td>";
echo "<td>" . $row['specialAbilities'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Additional Info: </td>";
echo "<td>" . $row['additionalInfo'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
mysqli_close($con);
?>
答案 0 :(得分:1)
试试这个 -
$result = mysqli_query($con,"SELECT * FROM weapons");
while($row = mysqli_fetch_array($result))
{
$row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO";
echo "<center>";
echo "<table border='1' class='display'>";
echo "<tr>";
echo "<td>Weapon Name: </td>";
echo "<td>" . $row['weaponName'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Creator: </td>";
echo "<td>" . $row['creator'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Category: </td>";
echo "<td>" . $row['weaponCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Sub-Category: </td>";
echo "<td>" . $row['weaponSubCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Cost: </td>";
echo "<td>" . $row['costAmount'] . " " . $row['costType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(S): </td>";
echo "<td>" . $row['damageS'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(M): </td>";
echo "<td>" . $row['damageM'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Critical: </td>";
echo "<td>" . $row['critical'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Range Increment: </td>";
echo "<td>" . $row['rangeIncrement'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weight: </td>";
echo "<td>" . $row['weight'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Type: </td>";
echo "<td>" . $row['weaponType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Masterwork: </td>";
echo "<td>" . $row['masterwork'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Special Abilities: </td>";
echo "<td>" . $row['specialAbilities'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Additional Info: </td>";
echo "<td>" . $row['additionalInfo'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
mysqli_close($con);
?>