Maison >développement back-end >tutoriel php > mysql_query() 实施结果一直为false

mysql_query() 实施结果一直为false

WBOY
WBOYoriginal
2016-06-13 13:10:271195parcourir

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)) {
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn