我正在上一個測試課,主要是自學。下面是類:OOP PHP頭等級Mysql數據庫連接
class Connection
{
public $con;
public $dbSelected;
public $activeConnection;
public $dataBaseName;
function __contruct($dbUserName, $dbPassword, $server = "localhost")
{
$this->con = mysql_connect($server,$dbUserName,$dbPassword);
if(!$this->$con)
{
$this->activeConnection = false;
}
else
{
$this->activeConnection = true;
}
}
public function dbConnect($dbName, $identifyer = null)
{
if ($identifyer === null)
{
$identifyer = $this->con;
}
$this->dbSelected = mysql_select_db($dbName, $identifyer);
$this->dataBaseName = $dbName;
if($this->dbSelected != true)
{
$this->connectionErrorReport(__LINE__);
}
}
public function cleanData(array $submission)
{
unset($submission["throughTheCleaners"]);
foreach($submission as $key => $value)
{
if(is_array($value))
{
$data[$key] = $this->cleanData($value);
}
else
{
$data[$key] = mysql_real_escape_string($value);
}
}
$data["throughTheCleaners"] = true;
return $data;
}
public function query($query, $dataSent)
{
if($dataSent["throughTheCleaners"] != true)
{
die("you must clean the data".__LINE__);
}
if($this->activeConnection == true && $this->dbSelected == true)
{
$result = mysql_query($query) or queryErrorReport($query, __LINE__);
$i = 0;
while($row = mysql_fetch_array($result))
{
foreach($row as $key => $value)
{
$data[$i][$key] = $value;
$i++;
}
}
return $data;
}
else
{
$this->$connectionErrorReport(__LINE__);
}
}
public function connectionErrorReport($line = __LINE__)
{
$error = "There has been a connection error on line ".$line."</br>";
if($this->activeConnection == false)
{
$error.= "Active Connection Error <br/>";
}
if($this->dbSelected == false)
{
$error.= "Data Base Selection Error <br/>";
}
die($error.mysql_error());
}
public function queryErrorReport($query, $line = __LINE__)
{
die("There was a query error on ".$line."<br />$query<br/>".mysql_error());
}
function __destruct() {
mysql_close($this->con);
}
}
我不能爲我找出它爲什麼不會在構造器調用期間拿起存儲在$con
變量mysql數據庫資源及其他功能使用的壽命。
最終會有另一個類中字符串的加密函數,但是我目前遇到的唯一錯誤是屬於連接類。
,因此引發錯誤的頁面看起來是這樣的:
include 'connection.php';
include 'loginClass.php';
$longinConnection = new Connection('***USERNAMEHERE***','***PASSWORD***');
$longinConnection->dbConnect("***DBTOCONNECTTO***");
echo Authoriz::encryptPassword("***USERPASSWORD***",$longinConnection);
本頁面測試encryptPassword
功能,但我甚至無法獲得Connection
類的工作。它引發的錯誤是如下:
警告:mysql_select_db()預計參數2是資源,在[路徑]空給出第27行 已經有上線31 主動連接錯誤 數據連接錯誤基準選擇錯誤
警告:mysql_close()預計參數1是資源,在[路徑]空給出上線103
在我看來,該$con
變量沒有被設置出於某種原因的任何洞察力到我做錯了什麼將不勝感激。
你真應該看看[PDO](http://php.net/pdo)。程序性的'mysql_ *'API更是10歲。你不應該用它寫新的代碼。 – 2012-03-14 19:38:15