Home Backend Development PHP Tutorial mysql_query() 实施结果一直为false

mysql_query() 实施结果一直为false

Jun 13, 2016 pm 01:10 PM
connection protected quot this

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!';
        }
    }

?>

Copy after login


------
派生类: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);
        }
    }

?>

Copy after login


----
使用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"];
?>

Copy after login


------解决方案--------------------
mysql_error看一下就知道了
------解决方案--------------------
public function __construct($connection,$close_flag){
$this->connection = $connection;
$this->connection = $close_flag;
}
这么严重的错误都看不出来?

另外
if (!mysql_select_db($this->name,$this->connection)) {
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does CONNECTION_REFUSED mean? What does CONNECTION_REFUSED mean? Jul 31, 2023 pm 02:48 PM

CONNECTION_REFUSED is a network connection error that usually occurs when trying to connect to a remote server. When a client device attempts to establish a network connection with a server, and the server rejects the connection request, a CONNECTION_REFUSED error is returned. Common reasons include: the server is not started, the server cannot accept more connection requests, the server firewall blocks the connection, etc.

How to solve connection error How to solve connection error Nov 07, 2023 am 10:44 AM

Solution: 1. Check the network connection; 2. Check the server status; 3. Clear the cache and cookies; 4. Check the firewall and security software settings; 5. Try to use other networks, etc.

PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused Jun 23, 2023 am 08:54 AM

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

In Java, can we declare a top-level class as protected or private? In Java, can we declare a top-level class as protected or private? Sep 12, 2023 pm 07:21 PM

No, we cannot declare top-level classes as private or protected. It can be public or default (no modifiers). If there are no modifiers, there should be default access. Syntax //Atoplevelclass publicclassTopLevelClassTest{ //Classbody} If a top-level class is declared as private, the compiler will report an error, prompting "The modifier private is not allowed here." This means that top-level classes cannot be private, the same applies to protected access

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

How to use this method in Java How to use this method in Java Apr 18, 2023 pm 01:58 PM

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

See all articles