2014-06-10 104 views
0

我有這個簡單的程序,存儲和檢索數據庫中的文件,我可以運行插入文件,但我似乎無法運行代碼檢索文件。無法運行PHP代碼從MSSQL數據庫檢索數據

我使用MSSQL爲我的數據庫

下面是測試連接(process1.php)代碼:

<?php 

class Connection { 

    public $conn; 

    public function connectDatabase() { 
     $serverName = "localhost"; 
     $uid = "sa"; 
     $pwd = "joseph04"; 
     $databaseName = "Profile"; 

     $connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>$databaseName); 

     // Connect using SQL Server Authentication 


     $this->conn = sqlsrv_connect($serverName, $connectionInfo); 

     // Test Connection 
     if($this->conn === false) 
     { 
      echo "Connection could not be established.\n"; 
      die(print_r(sqlsrv_errors(), true)); 
     } 
    } 
} 

?> 

,這裏是在數據庫中(ShowProcess檢索數據的代碼。 PHP):

<?php 

include_once("process1.php"); 

class showData extends Connection { 
    public function doShowData(){ 
    //declare the SQL statement that will query the database 
     $query = "SELECT col1, col2 "; 
     $query .= "FROM dbo.ProfileTable "; 

    //execute the SQL query and return records 
     $result = sqlsrv_query($this->conn, $query) 
      or die(print_r(sqlsrv_errors(), true)); 

    //Show results in table 

    $o = '<table id="myTable"> 
      <thead> 
      <tr> 
      <th>Col 1</th> 
      <th>Col 2</th> 
      </tr> 
      </thead><tbody>'; 

      while ($record = sqlsrv_fetch_array($result)) 
       { 
        $o .= '<tr><td>'.$record ['col1'].'</td><td>'.$record ['col2'].'</td></tr>'; 
       }    

      $o .= '</tbody></table>'; 

      echo $o; 



     //Show result from sql table separated by comma (commented out) 
      /* while ($record = mssql_fetch_array($result)) 
      { 
       echo $record["col1"] . " , " . $record["col2"] . "<br />"; 
      } */ 

     //free result set memory 
      sqlsrv_free_stmt($result); 

     //close the connection 
      sqlsrv_close($this->conn); 
    } 
} 

if (isset($_POST['formView'])){ 
    $i = new showData; 
    $i->connectDatabase(); 
    $i->doShowData(); 
} 

?> 

這是我的錯誤代碼:

Array ([0] => Array ([0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'col1'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'col1'.) [1] => Array ([0] => 42S22 [SQLSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'col2'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'col2'.)) 

我想我已經搞砸與sqlsrv_query()參數

請幫助?我只是PHP和MSSQL的新手。 謝謝!

此外,我從@klcant git代碼..介意如果我使用您的代碼?謝謝!

+0

檢查列名或只是$ query =「SELECT col1,col2」;替換爲$ query =「SELECT *」;僅用於調試目的 – Abhishek

+0

當我使用*進行更改時,出現錯誤消息,指向'$ o。='​​'。$ record ['col1']。'​​'。$ record ['col2']。''; while while循環 –

+0

在while循環中註釋此行$ o。='​​'。$ record ['col1']。'​​'。$ record ['col2']。'';並使用print_r($ record);只是爲了看看它給出了什麼 – Abhishek

回答

0

@Abhishek這裏是輸出:

好的,我已經做了,這是輸出:

陣列([0] => [名稱] => [1] => 0 [數組([0] => Cayas [姓名] => Cayas [1] => 35 [年齡] => 35 [2] =>女性[性別] [性別] => 0 [2] => [性別] => Array [(0] => Celina [Name] => Celina [1] => 19 [年齡] => 19 [2] =>女性[Sex] =>女性)Array([0] => > Chala [Name] => Chala [1] => 90 [年齡] => 90 [2] =>女性[Sex] =>女性)Array([0] => Inna [Name] => => 19 [年齡] => 19 [2] =>女性[性別] =>女性)Array([0] => Jenina [Name] => Jenina [1] => 19 [Age] => 19 [2 ] =>女性[性別] =>女性)Array([0] => John [Name] => John [1] => 12 Array([0] => Joseph [Name] => Joseph [1] => 19 [Age] => 19 [2] => [年齡] => 12 [2] =>男性[Sex] =>男性[性別] =>男性)陣列([0] =>女士[姓名] =>女士[1] => 0 [年齡] => 0 [2] =>女性[性別] => 0] => Mary [Name] => Mary [1] => 28 [Age] => 28 [2] =>女性[Sex] =>女性) Col 1 Col 2

+0

所以現在在你的數據庫中沒有字段命名col1或col2首先要注意的是,現在你想要顯示哪些字段? (年齡,姓名,性別)還是全部?讓我知道。 – Abhishek

+0

所有這些如果可能:) –

0

好吧,代碼,它可能會解決你的問題 -

public function doShowData(){ 
    //declare the SQL statement that will query the database 
     $query = "SELECT * "; 
     $query .= "FROM dbo.ProfileTable "; 
//execute the SQL query and return records 
    $result = sqlsrv_query($this->conn, $query) 
     or die(print_r(sqlsrv_errors(), true)); 

//Show results in table 

$o = '<table id="myTable"> 
     <thead> 
     <tr> 
     <th>Name</th> 
     <th>Age </th> 
     <th>Sex</th> 
     </tr> 
     </thead><tbody>'; 

     while ($record = sqlsrv_fetch_array($result)) 
      { 

       $o .= '<tr><td>'.$record ['Name'].'</td><td>'.$record ['Age'].'</td><td>'.$record ['Sex'].'</td></tr>'; 
      }    

     $o .= '</tbody></table>'; 

     echo $o; 



    //Show result from sql table separated by comma (commented out) 
     /* while ($record = mssql_fetch_array($result)) 
     { 
      echo $record["col1"] . " , " . $record["col2"] . "<br />"; 
     } */ 

    //free result set memory 
     sqlsrv_free_stmt($result); 

    //close the connection 
     sqlsrv_close($this->conn); 


} 
+0

Yey!謝謝@Abhishek! :) –

相關問題