我有一个简单的 mysql/php 问题。所以我为我的网站添加图像标题,代码如下所示。它可以工作,但是当您不放置图像时,它会显示为空白。我需要它显示为“无图像标题”(因为我将使用它来进行图像描述)。它基本上获取图像名称,然后从该行获取标题。 那么我该怎么做呢? :/我对 PHP 还很陌生。
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
}
else {
echo 'no image title';
}
?>
最佳答案
像这样更改 while 循环:
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
另外,我不确定 $imgtitleset
变量的用途,但您可能可以摆脱 if 语句检查它是否已设置。
编辑:整个事情可能应该是这样的:
<?php
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
?>
关于php - MySQL PHP 记录检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17626726/