如果其他情况不起作用

时间:2015-03-18 07:54:41

标签: php mysql

我需要什么

  • 我需要类似于count为零的控制流应该移动到else条件。

php code

$lat = $_GET['lat'];
$long = $_GET['lng'];
 /* new condition implemented */
 $city=$_GET['city'];
 $country=$_GET['country'];

 if(isset($city) && isset($country))
{

        $query="SELECT city.id cityid,city.name city_name
        FROM city
        WHERE city.name ='$city' limit 1";
        $resul = mysql_query($query);
        $num=mysql_numrows($resul);
        $rows = mysql_fetch_row($resul);

       /* check for the city name not matched with databse name */
       if($num > 0)
       {

         /* now check for the upcoming events */
            $q="SELECT * 
            FROM event
            INNER JOIN event_edition ON event.id = event_edition.event
            WHERE event.city =".$rows[0]."
            AND event_edition.end_date >=NOW( )";
            $r = mysql_query($q);
            $nums=mysql_numrows($r);
            $rowse = mysql_fetch_row($r);

                    /* check for count of upcoming events */
                    if($nums!=='' && $nums!=='null' && $nums >0)
                    {

                                $query2 =  "SELECT city.id,city.name,city.url,country.id,country.name,country.phonecode,country.url,country.currency FROM city,country
                                WHERE city.id = ".$rows[0]." AND country.id = city.country";
                                $result = mysql_query($query2);

                                        if($row = mysql_fetch_row($result)) 
                                        {
                                                        $city = array();
                                                        $city['id'] = $row[0];
                                                        $city['text'] = $row[1];
                                                        $city['url'] = $row[2];

                                                        $country = array();
                                                        $country['id'] = $row[3];
                                                        $country['text'] = $row[4];
                                                        $country['phone_code'] = $row[5];
                                                        $country['country_url'] = $row[6];
                                                        $country['currency'] = $row[7];

                                                        $cityData = array();
                                                        $cityData['city'] = $city;
                                                        $cityData['country'] = $country;
                                                        echo json_encode($cityData);
                                        }
                    }

      }



}

else
{

//this should be true when count if($num > 0) //
}

输出:

  • 当count fetch数据库为零时,它会将控制流转移到else循环。
  • 它应该是else循环中的数据。

2 个答案:

答案 0 :(得分:0)

$num=mysql_numrows($resul);不存在,正确的名称为mysql_num_rows,带有下划线。

$num = mysql_num_rows($resul);
                ^

而其他分支落后于坏支架(接近if(isset($city) && isset($country))条件)。

if(isset($city) && isset($country)) {
    ...
    if ($num > 0) {
        ...
    } else {
        // $num == 0
    }
} // you have ELSE branch here

答案 1 :(得分:0)

您需要使用两个赋值运算符才能生成if条件。你最内层的if条件只有一个赋值运算符。

if($row= =mysql_fetch_row...)
{.........}