Я пытаюсь написать класс соединений PHP, который я могу легко использовать в нашем приложении.
В настоящее время я работаю над скалярной командой Execute.
Мне нужно вернуть один столбец и одну строку.
Вот что у меня так далеко. И да, я знаю, что учетные данные должны быть во внешнем файле.
Я получаю ошибки:
[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');
Задача ещё не решена.
Других решений пока нет …