1)现有一个数据库名为test,里面只有一个表student。
属性名称:ID, Name, Email.
2)尝试着将数据库连接与操作封装成一个类DatabaseManager,并扩展了一个类StudentDetailsDataManager来获取学生信息。
3)问题:能够连接到test数据库,sql语句在数据库中测试过没有问题,但mysql_query()执行sql语句结果一直为false。不知什么问题?代码如下:
数据库操作基类:DatabaseManager//DatabaseManager.php
<?php class DatabaseManager{
protected $host;
protected $name;
protected $user;
protected $psw;
protected $connection;
protected $close_flag;
public function __construct($connection,$close_flag){
$this->connection = $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//StudentDetailsDataManager.php
<?php
require 'DatabaseManager.php';
class StudentDetailsDataManager extends DatabaseManager{
public function __construct($connection="",$close_flag=true){
parent::__construct($connection, $close_flag);
$this->host = "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
require_once 'StudentDetailsDataManager.php';
$stuDataManager = new StudentDetailsDataManager();
$ID = "DA123456"; $data=NULL;
$stuDataManager->getStudentInfo($ID, $data);
$stuDataManager->db_close();
echo $data["ID"];
?>
属性名称:ID, Name, Email.
2)尝试着将数据库连接与操作封装成一个类DatabaseManager,并扩展了一个类StudentDetailsDataManager来获取学生信息。
3)问题:能够连接到test数据库,sql语句在数据库中测试过没有问题,但mysql_query()执行sql语句结果一直为false。不知什么问题?代码如下:
数据库操作基类:DatabaseManager//DatabaseManager.php
<?php class DatabaseManager{
protected $host;
protected $name;
protected $user;
protected $psw;
protected $connection;
protected $close_flag;
public function __construct($connection,$close_flag){
$this->connection = $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//StudentDetailsDataManager.php
<?php
require 'DatabaseManager.php';
class StudentDetailsDataManager extends DatabaseManager{
public function __construct($connection="",$close_flag=true){
parent::__construct($connection, $close_flag);
$this->host = "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
require_once 'StudentDetailsDataManager.php';
$stuDataManager = new StudentDetailsDataManager();
$ID = "DA123456"; $data=NULL;
$stuDataManager->getStudentInfo($ID, $data);
$stuDataManager->db_close();
echo $data["ID"];
?>
谢谢提醒,使用了mysql_error(),但没有输出任何信息。逻辑出问题了?但程序看似没有什么错误。
直接执行sql是可以的。刚才又调试了一下,发现在程序中使用数据库名称限定选择的表就okay了,也就是使用“select * from test.student where ID = $ID" 。 问题貌似出现在选择数据库上,但一直没想通为什么会出错。
$this->connection = $connection;
$this->connection = $close_flag;
}
这么严重的错误都看不出来?另外
if (!mysql_select_db($this->name,$this->connection)) {
$this->da_handle_select();
return false;
}
protected function db_handle_select(){
echo 'Failed access database!';
}
//$query = "SELECT * FROM student WHERE ID ='$ID'";
$query = "select * from student where ID = '$ID'";
$result = mysql_query($query);
//print_r($result);
if (!$result) { //$result是一个结果集,不是布尔类型放在if里面有意义吗??
echo "result is empty!!";
return false;
}