请有人可以向我解释如何调整我的代码以使其如果mysql表中不存在记录/值它会回显一段文本?谢谢。
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\"></a>";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
?>
答案 0 :(得分:1)
使用三元运算符'?:'
样品:
$you_var ?: 'you_text_if_not_exists'
答案 1 :(得分:1)
检查你的变量:
<?php (isset($reviews['display_name']) ? $reviews['display_name'] : "entry doesn't exists"; ?>
答案 2 :(得分:0)
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews = mysql_fetch_array($reviews_set))>=1)
{
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\"></a>";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
}
} else {
echo 'No reviews available';
}
?>
答案 3 :(得分:0)
如果要检查表是否有行,请使用以下命令:
$num_rows = mysql_num_rows($reviews_set);
$num_rows
将包含行数。
答案 4 :(得分:0)
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews_set) > 0) {
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\"></a>";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
<?
}
}else{
echo "No Data";
}
?>
我希望这会有所帮助