2012-05-11 50 views
1

我真的不知道我在做什麼錯。我想要統計查詢中的行。我的ID從1開始到...第1步,所以最大的數字是最後一個記錄。我只想讓腳本在de DB的最後一個記錄上運行。所以這是我的代碼。mysqli_num_rows()期望參數1是mysqli_result地理位置

public function saveLongLan() 
    { 
     define("MAPS_HOST", "maps.google.com"); 
     define("KEY", "MYKEY"); 

     $link = mysql_connect('localhost', 'root', 'root'); 
     if (!$link) { 
     die('Not connected : ' . mysql_error()); 
     } 

     // Set the active MySQL database 
     $db_selected = mysql_select_db('Foodsquare', $link); 
     if (!$db_selected) 
     { 
      die("Can\'t use db : " . mysql_error()); 
     } 

     $query = "SELECT * FROM tblPlaces WHERE 1"; 
     $result = mysql_query($query); 
     $row_cnt = mysqli_num_rows($result); 


      if (!$result) 
       { 
        die("Invalid query: " . mysql_error()); 
       } 


     $delay = 0; 
     $base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY; 

     while ($row = @mysql_fetch_assoc($result)) 
      { 
       $geocode_pending = true; 

        while ($geocode_pending) 
         { 
          $address = $row["Street"]; 
          $id = $row["Id"]; 
          $request_url = $base_url . "&q=" . urlencode($address); 
          $xml = simplexml_load_file($request_url) or die("url not loading"); 

          $status = $xml->Response->Status->code; 
          if (strcmp($status, "200") == 0) 
           { 
             // Successful geocode 
             $geocode_pending = false; 
             $coordinates = $xml->Response->Placemark->Point->coordinates; 
             //explode functie breekt een string af vanaf een bepaald teken en stopt hem dan in een array 
             $coordinatesSplit = explode(',', $coordinates); 
             // formaat: Longitude, Latitude, Altitude 
             $lat = $coordinatesSplit[1];//getal = plaats in array 
             $lng = $coordinatesSplit[0]; 


             $query = sprintf("UPDATE tblPlaces " . 
               " SET lat = '%s', lng = '%s' " . 
               " WHERE id = '". $row_cnt . "' LIMIT 1;", 
              mysql_real_escape_string($lat), 
              mysql_real_escape_string($lng), 
              mysql_real_escape_string($id)); 
             $update_result = mysql_query($query); 
              if (!$update_result) 
              { 
              die("Invalid query: " . mysql_error()); 
              } 
           } else if (strcmp($status, "620") == 0) 
            { 
              // sent geocodes too fast 
              $delay += 100000; 
            } 
            else 
            { 
             // failure to geocode 
             $geocode_pending = false; 
             echo "Address " . $address . " failed to geocoded. "; 
            echo "Received status " . $status . " \n"; 
            } 
           usleep($delay); 
         } 
      } 

    } 

回答

0

你的查詢不正確$query = "SELECT * FROM tblPlaces WHERE 1";

您應該在where子句後面指定列名稱,以便與例如:WHERE column_name = 1之類的值進行比較。

+0

我不這麼認爲,查詢我運行得很好,沒有rowcount,它的副本從谷歌api過去。謝謝 – Niels

+0

因爲我忘記提及where子句的條件,所以我遇到了類似的問題。轉到本網站獲取關於where子句的教程:[where子句教程](http://www.tutorialspoint.com/mysql/mysql-where-clause.htm) – shivgre

-1

您正在使用mysqli_num_rows($ result),但未使用MySQLi。

變化:

$row_cnt = mysqli_num_rows($result); 

要:

$row_cnt = mysql_num_rows($result); 

,你應該是好去。

+1

好的,謝謝你,我要把我的代碼改成mysqli。 – Niels

+1

請停止使用古老的'mysql_ *'函數編寫新代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是一個很好的PDO相關教程](http://goo.gl/vFWnC)。 – vascowhite

+0

@vascowhite對不起,我試圖將此轉換爲mysqli在這個時候 – Niels

相關問題