我的查詢是:query_id返回任何
$sqlCommand="select `ID` from `freecomputermarket`.`members` where `UserName`='$this->_userName';";
我正在usign XAMPP的Apache服務器和我的工作端口(89)
這個類負責數據庫的連接:
<?php
class MySql
{
private $_link_Id,$_query_Id,$_serverName,$_userName,$_password,$_dbName,$_rowNum;
public function __construct()
{
$this->_serverName="localhost";
$this->_userName="root";
$this->_password="";
$this->_dbName="freecomputermarket";
}
public function connect()
{
$this->_link_Id=mysql_connect($this->_serverName,$this->_userName,$this->_password);
if(!$this->_link_Id)
{
exit("The Connect is Failed");
}
$db_select=mysql_select_db($this->_dbName,$this->_link_Id);
if(!$db_select)
{
exit("Can't Select DataBase");
}
}
public function query($sqlcommand)
{
$sqlcommand= addslashes($sqlcommand);
//echo $sqlcommand;
$this->_query_Id=mysql_query($sqlcommand,$this->_link_Id);
exit($this->_query_Id);//print it to check if it is available.
if(!$this->_query_Id)
exit("Query failed");
$this->_rowNum=mysql_affected_rows();
}
public function getRow()
{
if($this->_rowNum)
{
return mysql_fetch_assoc($this->_query_Id);
}
}
public function getAllRows()
{
$arr=array();
$count=0;
while($count<$this->_rowNum)
{
array_push($arr,$this->GetRow());
$count++;
}
return $arr;
}
public function getAffectedRowsNumber()
{
return $this->_rowNum;
}
}
?>
這段代碼用於連接到mysql dbms並執行查詢。 打印$ _link_Id時,它有一個值。 打印$ _query_Id時,它什麼都沒有?
請正確縮進你的代碼,其他人需要閱讀它。同時使用'var_dump'進行調試,而不是'exit'。 – hakre
謝謝hakra告訴我關於var_dump的信息 –
如果你正在尋找一些基於mysql_ *'函數的數據庫類,在[這個答案]中有一個(http://stackoverflow.com/a/11580420/367456)包含錯誤處理以及可輕鬆擴展的結果對象。正如下面的評論,'addslashes'不屬於'query'方法。如果你想讓編寫SQL查詢更加舒適,可以添加另一個構建這些字符串的類,比較如下:http://stackoverflow.com/a/12221284/367456(這不完美,但可能會給你一些指示)。 – hakre