2016-03-20 48 views
2
<?php  
$serverName = "servername"; 
$connectionInfo = array("Database"=>"DB", "UID"=>"user1", "PWD"=>"[email protected]"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 
if($conn) { 
    echo "Connection established.<br />"; 
} 
else{ 
    echo "Connection could not be established.<br />"; 
    die(print_r(sqlsrv_errors(), true)); 
} 
$sql = "SELECT 
     COUNT(CASE WHEN online = 'Yes' THEN city END) AS o_total, 
     COUNT(CASE WHEN online = 'No' THEN city END) AS o_offline, 
     COUNT(*) as t_total, 
     COUNT(CASE WHEN online = 'Yes' AND channel='Name1' THEN city END) AS o_Name1, 
     COUNT(CASE WHEN channel='Name1' THEN city END) AS t_Name1, 
     COUNT(CASE WHEN online = 'YES' AND channel='Name2' THEN city END) AS o_Name2, 
     COUNT(CASE WHEN channel='Name2' THEN city END) AS t_Name2, 
     COUNT(CASE WHEN online = 'Yes' AND channel='Name3' THEN city END) AS o_Name3, 
     COUNT(CASE WHEN channel='Name3' THEN city END) AS t_Name3, 
     COUNT(CASE WHEN online = 'Yes' AND channel='Name4' THEN city END) AS o_Name4, 
     COUNT(CASE WHEN channel='Name4' THEN city END) AS t_Name4, 
     city FROM wmm group by city"; 
     $result = $conn->query($sql); 

     $array = new stdClass(); 
     if ($result->num_rows > 0) { 
      while($row = sqlsrv_fetch_assoc($result)) { 
       $array->$row['city'] = $row; 
      } 
      echo json_encode($array); 

     } 

?> 

我有錯誤=>致命錯誤:調用一個成員函數查詢()非對象上在C:\ XAMPP \ htdocs中\用戶\ File.php上線
呼叫一個成員函數查詢()非對象上

請幫我提前

+0

如果你使用'sqlsrv_query'功能,同時傳遞'$ conn'並在「連接建立」條件塊一個簡單的測試查詢嗎? http://php.net/manual/en/function.sqlsrv-query.php –

回答

0

解決this..thanks嘗試連接這樣:

<?php  
$dbservername= "servername"; 
$dbusername="user1"; 
$dbpassword="[email protected]"; 
$dbname="DB"; 
$conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbname); 
if($conn->connect_error) { 
    echo "Connection could not be established.<br />"; 
    die(print_r($conn->connect_error, true)); 
} 
else{ 
    echo "Connection established.<br />"; 

     $sql = "SELECT 
     COUNT(CASE WHEN online = 'Yes' THEN city END) AS o_total, 
     COUNT(CASE WHEN online = 'No' THEN city END) AS o_offline, 
     COUNT(*) as t_total, 
     COUNT(CASE WHEN online = 'Yes' AND channel='Name1' THEN city END) AS o_Name1, 
     COUNT(CASE WHEN channel='Name1' THEN city END) AS t_Name1, 
     COUNT(CASE WHEN online = 'YES' AND channel='Name2' THEN city END) AS o_Name2, 
     COUNT(CASE WHEN channel='Name2' THEN city END) AS t_Name2, 
     COUNT(CASE WHEN online = 'Yes' AND channel='Name3' THEN city END) AS o_Name3, 
     COUNT(CASE WHEN channel='Name3' THEN city END) AS t_Name3, 
     COUNT(CASE WHEN online = 'Yes' AND channel='Name4' THEN city END) AS o_Name4, 
     COUNT(CASE WHEN channel='Name4' THEN city END) AS t_Name4, 
     city FROM wmm group by city"; 
     $result = $conn->query($sql); 

     $array = new stdClass(); 
     if ($result->num_rows > 0) { 
      while($row = sqlsrv_fetch_assoc($result)) { 
       $array->$row['city'] = $row; 
      } 
      echo json_encode($array); 

     } 
} 


?> 
+0

對不起,但經過糾正一些事情後我得到=> mysqli :: mysqli():(HY000/2002):無法建立連接,因爲目標機器積極拒絕它。 in C:\ xampp \ htdocs \ user \ user1.php on line
ROCK

0

sqlsrv_connect()不會返回一個對象,而是一個資源。

您需要致電sqlsrv_query()並將$conn資源作爲第一個參數,將查詢作爲第二個參數傳遞。

所有PHP sqlsrv函數的文檔here

相關問題