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

时间:2011-08-22 04:04:03

标签: php sql

$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!";

4 个答案:

答案 0 :(得分:1)

mysql_fetch_field返回包含字段信息的对象。要获取sql查询结果,您应该使用mysql_fetch_rowmysql_fetch_assoc之类的内容。

最好的方法是检查php manual

答案 1 :(得分:1)

这应该这样做:

$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!";
}

答案 2 :(得分:0)

$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!';
}

答案 3 :(得分:0)

试试这个:

$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!";
}