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 t开发者_开发知识库he 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!";
This should do it:
$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
returns an object containing field information. For get the sql query result, you should use something like mysql_fetch_row
or mysql_fetch_assoc
.
The best thing is to check the php manual.
$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!';
}
Try this:
$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!";
}
精彩评论