mysql_query() 執行結果一直為false
1)現有一個資料庫名為test,裡面只有一個表student。
屬性名稱:ID, Name, Email.
2)嘗試著將資料庫連接與操作封裝成一個類DatabaseManager,並擴充了一個類StudentDetailsDataManager來擷取學生資訊。
3)問題:能夠串連到test資料庫,sql語句在資料庫中測試過沒有問題,但mysql_query()執行sql語句結果一直為false。不知什麼問題?
代碼如下:
資料庫操作基類:DatabaseManager
PHP code
//DatabaseManager.phpconnection = $connection; $this->connection = $close_flag; } protected function db_open(){ if(empty($this->connection)){ $this->connection = mysql_connect($this->host,$this->user,$this->psw); if (!$this->connection) { $this->db_handle_error_connetion(); return false; } if (!mysql_select_db($this->name,$this->connection)) { $this->da_handle_select(); return false; } } } public function db_close(){ if($this->connection) mysql_close($this->connection); } protected function db_handle_error_connetion(){ echo 'Failed connetion'; } protected function db_handle_select(){ echo 'Failed access database!'; } }?>
------
衍生類別:StudentDetailsDataManager
PHP code
//StudentDetailsDataManager.phphost = "localhost"; $this->user = "root"; $this->psw = "root"; $this->name = "test"; $this->db_open(); } public function getStudentInfo($ID,&$data){ //$query = "SELECT * FROM student WHERE ID ='$ID'"; $query = "select * from student where ID = '$ID'"; $result = mysql_query($query); //print_r($result); if (!$result) { echo "result is empty!!"; return false; } $data = mysql_fetch_array($result,MYSQL_ASSOC); mysql_free_result($result); } }?>
----
使用StudentDetailsDataManager執行個體擷取學生資訊
PHP code
getStudentInfo($ID, $data); $stuDataManager->db_close(); echo $data["ID"];?>
------解決方案--------------------
mysql_error看一下就知道了
------解決方案--------------------
public function __construct($connection,$close_flag){
$this->connection = $connection;
$this->connection = $close_flag;
}
這麼嚴重的錯誤都看不出來?
另外
if (!mysql_select_db($this->name,$this->connection)) {