2016-03-20 10 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

hereを文書化しているあなたは、 '$のconn'とあなたの「接続確立」の条件付きブロックで簡単なテストクエリ右の両方を渡す' sqlsrv_query'機能を、使用している場合は? http://fp.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オンライン
ROCK

0

sqlsrv_connect()はオブジェクトではなくリソースを返します。

sqlsrv_query()に電話し、$connリソースを最初のパラメータとして、クエリを2番目のパラメータとして渡す必要があります。

すべてのPHP sqlsrv機能