-8

嗨,我在下面code使用city valuedatabase--

<?php
//echo "SELECT city FROM tbl_city_master WHERE id = ".$rs->city.""
$getCityQuery = mysql_query("SELECT city FROM tbl_city_master WHERE id = ".$rs->city."");
    $resultSetCityQuery = mysql_fetch_assoc($getCityQuery);
?>
<? echo '<strong>City-</strong>' ?><? echo $resultSetCityQuery['city'];?>

我只需要在条件下说不选择城市——if else我该如何实现该代码

4

6 回答 6

1

您可以使用以下代码执行此操作:

$results_count = mysql_num_rows($resultSetCityQuery);
if ($results_count > 0) {
   // do something
} else {
   echo 'No city Choosen';
}

或者您可以使用 mysql_fetch_assoc 并检查是否为假:

$results = mysql_fetch_assoc($resultSetCityQuery);
if ($results == false) {
  echo 'No city Choosen';
} else {
  // do somthing
}
于 2013-01-15T08:35:48.423 回答
0
if (count ($resultSetCityQuery) === 0) echo "No city choosen"
      else echo $resultSetCityQuery['city'];

或者

if (empty ($resultSetCityQuery)) echo "No city choosen"
      else echo $resultSetCityQuery['city'];
于 2013-01-15T08:28:36.383 回答
0

强烈建议您停止使用 mysql_* 函数,因为它们将在即将发布的 php 版本中被弃用

您可以检查是否设置了mysql结果

if (count($resultSetCityQuery)>0) {
    echo $resultSetCityQuery['city'];
} else { 
    echo 'No city chosen';
}
于 2013-01-15T08:29:05.640 回答
0

试试这个:

foreach($resultSetCityQuery as $city){
    if(isset($city['city']) && !empty($city['city'])){
           echo "<strong>City-</strong> {$city['city']} ";
    }   
 }
于 2013-01-15T08:29:43.740 回答
0

如果可用,此代码将显示城市

<?php
$getCityQuery = mysql_query("SELECT city FROM tbl_city_master WHERE id = ".$rs->city."");
if ($getCityQuery)
    $resultSetCityQuery = mysql_fetch_assoc($getCityQuery);

?>
<?php
if (resultSetCityQuery != null)
echo "<strong>City-</strong>$resultSetCityQuery['city']";
?>
于 2013-01-15T08:30:17.647 回答
0

试试下面的代码

    if(!!$resultSetCityQuery) {
        echo $resultSetCityQuery['city'];
    } else {
        echo "no data found";
    or 
        echo mysql_error($getCityQuery);//if there is an error ragarding to your sql statement
    }
于 2013-01-15T08:36:57.683 回答