首页 > 后端开发 > php教程 > mysql_query() 实施结果一直为false

mysql_query() 实施结果一直为false

WBOY
发布: 2016-06-13 13:10:27
原创
1193 人浏览过

mysql_query() 执行结果一直为false
1)现有一个数据库名为test,里面只有一个表student。
属性名称:ID, Name, Email.
2)尝试着将数据库连接与操作封装成一个类DatabaseManager,并扩展了一个类StudentDetailsDataManager来获取学生信息。
3)问题:能够连接到test数据库,sql语句在数据库中测试过没有问题,但mysql_query()执行sql语句结果一直为false。不知什么问题?

代码如下:
数据库操作基类:DatabaseManager

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
//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
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
//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 code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php require_once 'StudentDetailsDataManager.php';
    $stuDataManager = new StudentDetailsDataManager();
    $ID = "DA123456"; $data=NULL;
    $stuDataManager->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)) {
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板