我有这个简单的程序来存储和检索数据库中的文件,我可以运行文件的插入,但我似乎无法运行代码冷却检索文件。
我正在使用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中获取代码。谢谢!
答案 0 :(得分:0)
@Abhishek这里是输出:
好的,我已经做到了,这就是输出:
数组([0] =&gt; [名称] =&gt; [1] =&gt; 0 [年龄] =&gt; 0 [2] =&gt; [性别] =&gt;)数组([0] = &gt; Cayas [名称] =&gt; Cayas [1] =&gt; 35 [年龄] =&gt; 35 [2] =&gt;女性[性别] =&gt;女性)数组([0] =&gt; Celina [名称] ] =&gt; Celina [1] =&gt; 19 [年龄] =&gt; 19 [2] =&gt;女性[性别] =&gt;女性)数组([0] =&gt; Chala [名称] =&gt; Chala [1] =&gt; 90 [年龄] =&gt; 90 [2] =&gt;女性[性别] =&gt;女性)数组([0] =&gt; Inna [名称] =&gt; Inna [1] =&gt ; 19 [年龄] =&gt; 19 [2] =&gt;女性[性别] =&gt;女性)数组([0] =&gt; Jenina [姓名] =&gt; Jenina [1] =&gt; 19 [年龄] =&gt; 19 [2] =&gt;女性[性别] =&gt;女性)数组([0] =&gt;约翰[姓名] =&gt;约翰[1] =&gt; 12 [年龄] =&gt; 12 [ 2] =&gt;男性[性别] =&gt;男性)数组([0] =&gt;约瑟夫[姓名] =&gt;约瑟夫[1] =&gt; 19 [年龄] =&gt; 19 [2] =&gt;男性[性别] =&gt;男性)数组([0] =&gt; Lady [姓名] =&gt; Lady [1] =&gt; 0 [年龄] =&gt; 0 [2] =&gt;女性[性别] = &gt;女性)数组([0] =&gt;玛丽[姓名] =&gt;玛丽[1] =&gt; 28 [年龄] =&gt; 28 [2] =&gt;女[性] =&gt;女) Col 1 Col 2
答案 1 :(得分: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);
}