search
HomeBackend DevelopmentPHP Tutorial mysql_query() 实施结果一直为false

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)) {
Statement
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
CONNECTION_REFUSED什么意思CONNECTION_REFUSED什么意思Jul 31, 2023 pm 02:48 PM

CONNECTION_REFUSED是一种网络连接错误,通常会在试图连接到远程服务器时出现。当客户端设备试图建立一个与服务器的网络连接时,如果服务器拒绝该连接请求,就会返回一个CONNECTION_REFUSED错误。常见的原因包括:服务器未启动、服务器无法接受更多的连接请求、服务器防火墙阻止了该连接等。

connection error怎么解决connection error怎么解决Nov 07, 2023 am 10:44 AM

解决方法:1、检查网络连接;2、检查服务器状态;3、清除缓存和Cookie;4、检查防火墙和安全软件设置;5、尝试使用其他网络等等。

PHP Warning: mysqli_connect(): (HY000/2002): Connection refused的解决方法PHP Warning: mysqli_connect(): (HY000/2002): Connection refused的解决方法Jun 23, 2023 am 08:54 AM

如果你使用PHP连接MySQL数据库时遇到了以下错误提示:PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused那么你可以尝试按照下面的步骤来解决这个问题。确认MySQL服务是否正常运行首先应该检查MySQL服务是否正常运行,如果服务未运行或者启动失败,就可能会导致连接被拒绝的错误。你可

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

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

在Java中,我们可以将顶级类声明为protected或private吗?在Java中,我们可以将顶级类声明为protected或private吗?Sep 12, 2023 pm 07:21 PM

不,我们不能将顶级类声明为私有或受保护。它可以是公共或默认(无修饰符)。如果没有修饰符,则应该具有默认访问权限。语法//Atoplevelclass&nbsp;&nbsp;publicclassTopLevelClassTest{&nbsp;&nbsp;&nbsp;//Classbody}如果将一个顶级类声明为私有(private),编译器将会报错,提示“在此处不允许使用修饰符private”。这意味着顶级类不能是私有的,同样也适用于protected访问

一篇搞懂this指向,赶超70%的前端人一篇搞懂this指向,赶超70%的前端人Sep 06, 2022 pm 05:03 PM

同事因为this指向的问题卡住的bug,vue2的this指向问题,使用了箭头函数,导致拿不到对应的props。当我给他介绍的时候他竟然不知道,随后也刻意的看了一下前端交流群,至今最起码还有70%以上的前端程序员搞不明白,今天给大家分享一下this指向,如果啥都没学会,请给我一个大嘴巴子。

聊聊Vue2为什么能通过this访问各种选项中属性聊聊Vue2为什么能通过this访问各种选项中属性Dec 08, 2022 pm 08:22 PM

本篇文章带大家解读vue源码,来介绍一下Vue2中为什么可以使用 this 访问各种选项中的属性,希望对大家有所帮助!

Java中this方法怎么使用Java中this方法怎么使用Apr 18, 2023 pm 01:58 PM

一、this关键字1.this的类型:哪个对象调用就是哪个对象的引用类型二、用法总结1.this.data;//访问属性2.this.func();//访问方法3.this();//调用本类中其他构造方法三、解释用法1.this.data这种是在成员方法中使用让我们来看看不加this会出现什么样的状况classMyDate{publicintyear;publicintmonth;publicintday;publicvoidsetDate(intyear,intmonth,intday){ye

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor