2013-01-15 113 views
-8

嗨,我使用下面code顯示來自databasecity value -如果在php else條件

<?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'];?> 

一切都是我需要說沒有哪座城市Choosen在if else condition--我如何可以實現代碼

回答

0

此代碼將顯示城市如果avaialable

<?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']"; 
?> 
+0

幾乎工作,而是拋出一個錯誤'警告:mysql_fetch_assoc()預計參數1是資源,boolen given' –

+0

檢查更新代碼 –

+0

Grt @非常感謝.. –

0
if (count ($resultSetCityQuery) === 0) echo "No city choosen" 
     else echo $resultSetCityQuery['city']; 

if (empty ($resultSetCityQuery)) echo "No city choosen" 
     else echo $resultSetCityQuery['city']; 
0

你強烈的廣告叼着停止使用mysql_ *功能,因爲他們將在即將到來的PHP版本

可以檢查MySQL的結果設置

if (count($resultSetCityQuery)>0) { 
    echo $resultSetCityQuery['city']; 
} else { 
    echo 'No city chosen'; 
} 
0

試試這個被棄用:

foreach($resultSetCityQuery as $city){ 
    if(isset($city['city']) && !empty($city['city'])){ 
      echo "<strong>City-</strong> {$city['city']} "; 
    } 
} 
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 
} 
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 
    }