如果这是PHP中的另一个值,如何获得mySQL字段回显语句


How to get an mySQL field echo a statment if this the value else if this is the other value in PHP

$query = "SELECT unitstat FROM tblunits where unitid='VDMRB1001'" ;
$result=mysql_query($query);
//unitstat is the field i'm trying to call
$field=("unitstat");      //is this correct??
while($unitstat = mysql_fetch_field($result))   //is this correct??
  if ($unitstat=="SOLD")
    echo "THIS UNIT IS SOLD!"; 
  else
    echo "THIS UNIT IS FOR SALE!";

应该这样做:

$query = "SELECT unitstat FROM tblunits where unitid='VDMRB1001'" ;
$result=mysql_query($query);
while($row = mysql_fetch_array($result)) 
{
  $unitstat = $row['unitstat'];
  if ($unitstat=="SOLD")
    echo "THIS UNIT IS SOLD!"; 
  else
    echo "THIS UNIT IS FOR SALE!";
}

mysql_fetch_field返回一个包含字段信息的对象。为了获得sql查询结果,您应该使用mysql_fetch_rowmysql_fetch_assoc之类的东西。

最好的方法是查看php手册

$query = "SELECT unitstat FROM tblunits where unitid='VDMRB1001'" ;
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
    echo 'THIS UNIT IS ' . ($row['unitstat'] == 'SOLD') ? 'SOLD!' : 'FOR SALE!';
}

试试这个:

$query  = "SELECT unitstat FROM tblunits where unitid='VDMRB1001'";           
$result = mysql_query($query) or die(mysql_error());
while(list($unitstat) = mysql_fetch_array($result))
{
if ($unitstat == "SOLD")
    echo "THIS UNIT IS SOLD!"; 
else
    echo "THIS UNIT IS FOR SALE!";
}