我正在尝试编写一个可以在我们的应用程序中轻松重用的PHP连接类。
目前我正在处理Execute scalar命令。
我需要返回一列和一行。
这是我到目前为止所拥有的。是的,我知道凭据应该在外部文件中。
我收到错误:
[08-Apr-2015 08:12:38 America/Los_Angeles] PHP Notice: Undefined offset: 0 in C:\Users\mhano00\Documents\Visual Studio 2012\PHPWebProject1\PHPWebProject1\includes\ConnectionClass.php on line 54
[08-Apr-2015 08:12:38 America/Los_Angeles] PHP Stack trace:
[08-Apr-2015 08:12:38 America/Los_Angeles] PHP 1. {main}() C:\U sers\mhano00\Documents\Visual Studio 2012\PHPWebProject1\PHPWebProject1\index.php:0
[08-Apr-2015 08:12:38 America/Los_Angeles] PHP 2. ConnectionClass->ExecuteScalar() C:\Users\mhano00\Documents\Visual Studio 2012\PHPWebProject1\PHPWebProject1\index.php:35
[08-Apr-2015 08:12:38 America/Los_Angeles] PHP Fatal error: Cannot access private property ConnectionClass::$stmt in C:\Users\mhano00\Documents\Visual Studio 2012\PHPWebProject1\PHPWebProject1\index.php on line 57
[08-Apr-2015 08:12:38 America/Los_Angeles] PHP Stack trace:
[08-Apr-2015 08:12:38 America/Los_Angeles] PHP 1. {main}() C:\Users\mhano00\Documents\Visual Studio 2012\PHPWebProject1\PHPWebProject1\index.php:0.".
这是连接类。
class ConnectionClass
{
private $serverName;
private $connectionInfo;
public $conn;
private $sql;
private $Result;
private $Rows;
private $stmt;
public function __construct()
{
$this->serverName = "XXX.XXX.XX.XX";
$this->connectionInfo = array( "Database"=>"XXXXX", "UID"=>"XXXXXX", "PWD"=>"XXXXXX");
$this->conn = sqlsrv_connect ($this->serverName, $this->connectionInfo);
if(!$this->conn === true)
{
//catches error on no connection to database, send auto email error
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
}
//may have to create a dataset within this class to be able to return each type of result.
public function ExecuteScalar($IncomingSql)
{
//select scalar, one cell value is returned
$this->sql = $IncomingSql;
$this->stmt = sqlsrv_query( $this->conn, $IncomingSql);
if ($this->stmt)
{
$this->Rows = sqlsrv_has_rows($this->stmt);
if ($this->Rows)
{
$row = sqlsrv_fetch_array( $this->stmt, SQLSRV_FETCH_ASSOC);
return $this->Result = $this->row[0];
}
}
}
public function CreateDataSet($sql)
{
//create Dataset, araay is returned
//can be called by this class or from outside to create a dataset array and return it
$this->sql = $incomingsql;
$stmt = sqlsrv_query( $conn, $sql);
if ($stmt)
{
//return array
}
}
public function SetServer($NewServer)
{
$this->serverName = $NewServer;
}
public function GetServer()
{
return $this->serverName;
}
}
die( print_r( sqlsrv_errors(), true));
}
}
//may have to create a dataset within this class to be able to return each type of result.
public function ExecuteScalar($IncomingSql)
{
//select scalar, one cell value is returned
$this->sql = $IncomingSql;
$stmt = sqlsrv_query( $this->conn, $IncomingSql);
if ($this->stmt)
{
$Rows = sqlsrv_has_rows($this->stmt);
if ($this->Rows)
{
$row = sqlsrv_fetch_array( $this->stmt, SQLSRV_FETCH_ASSOC);
$this->Result = $this->row[0];
}
}
}
public function SetServer($NewServer)
{
$this->serverName = $NewServer;
}
public function GetServer()
{
return $this->serverName;
}
}
然后在索引页面上,我正在调用这个类。
$conn2 = new ConnectionClass;
$conn = $conn2->conn;
$car = $conn2->ExecuteScalar('Select * from store');