search
HomeBackend DevelopmentPHP Tutorial应用MYSQLI读取数据库读不了问题

<?php  $DB_HOST = "";  $DB_USER = "";  $DB_PASSWORD = "";  $DB_DATABASE = "";      define("DB_HOST", "localhost");  define("DB_USER", "root");  define("DB_PASSWORD", "1234");    define("DB_DATABASE", "t6_db_hp");class mysqldb  {    var $host = DB_HOST;                   //localhost    var $user = DB_USER;                   //root    var $pass = DB_PASSWORD;               //1234    var $database = DB_DATABASE;           //t6_db_hp    var $conn;    var $sql;    var $n = 0;    var $m = 0;    var $result = "";    var $debug = false;    var $err = "";function mysqldb($host="", $user="", $pass="", $database="")	{      if ($host != "") $this -> host = $host;      if ($user != "") $this -> user = $user;      if ($pass != "") $this -> pass = $pass;      if ($database != "") $this -> database = $database;	       if (!$this -> conn = @mysqli_connect($this -> host, $this -> user, $this -> pass))	  {        return $this -> output("连接数据库 '".$this -> host."' 失败。".mysqli_error($this -> conn));      }	   mysqli_query($this -> conn,"SET NAMES 'utf8'");       $this -> selectdb();    }function selectdb($database="")	{      if ($database != "" && $database != $this -> database)        $this -> database = $database;      if (!mysqli_select_db($this -> conn, $this -> database))	  {        return $this -> output("无法使用数据库 '".$this -> database."'。");      }	}function query($sql="", $database="")                     //$sql	{      if ($sql != "") $this -> sql = $sql;      if ($database != "" && $database != $this -> database)	  {        $olddb = $this -> database;        $this -> selectdb($database);      }      $this -> result = @mysqli_query($this -> conn,$this -> sql);      if (mysqli_error($this -> conn) != "")	  {        return $this -> output("执行以下SQL语句时失败:'".$this -> sql."' <br><br>".mysqli_error($this -> conn));      }      $this -> n = @mysqli_affected_rows($this -> conn);                         if (!empty($olddb)) $this -> selectdb($olddb);                     return $this -> result;			}function get_data()	{   /*   if(!$this -> result || !preg_match("/^Resource/i", $this -> result)){    */                if(!$this -> result || !is_resource($this->result)){             return $this->output("没有数据,请先执行SQL的'select'语句!");      }      $count=0;      $this -> m = @mysqli_num_rows($this -> result);      if ($this -> m > 0)	  {        while ($row = mysqli_fetch_array($this -> result))		{          $data[$count] = $row;          $count++;        }        return $this->result=$data;      }	  else	  {        return $this -> output("数据为空!");      }      mysqli_free_result($this -> result);	}function output($msg){  $this->err=$msg;  if ($this->debug)echo $msg;  return false;}  }?><?php /*?>////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<?php */?><?php$db = new mysqldb();if($db){	echo "链接数据库 1";	echo "<br/>";	echo "<br/>";	} $sql = "SELECT * FROM t6_dept where de_id>'0' ORDER BY de_id ASC"; $db -> query($sql); $result = $db -> get_data(); if(!$result)       {	echo "取出数据失败result 0";	echo "<br/>";	echo "<br/>";	}echo $result[0]["de_name"];          //无法显示数据print_r($db);?>


最后输出不了数据$resul没有值  t浏览器显示结果:求哪里出错了 ???

链接数据库 1

取出数据失败result 0

mysqldb Object ( [host] => localhost [user] => root [pass] => 1234 [database] => t6_db_hp [conn] => mysqli Object ( [affected_rows] => 4 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: f373ea5dd5538761406a8022a4b8a374418b240e $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 4 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.20 [server_version] => 50620 [stat] => Uptime: 22428 Threads: 1 Questions: 5 Slow queries: 0 Opens: 68 Flush tables: 1 Open tables: 61 Queries per second avg: 0.000 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 1 [warning_count] => 0 ) [sql] => SELECT * FROM t6_dept where de_id>'0' ORDER BY de_id ASC [n] => 4 [m] => 0 [result] => mysqli_result Object ( [current_field] => 0 [field_count] => 4 [lengths] => [num_rows] => 4 [type] => 0 ) [debug] => [err] => 没有数据,请先执行SQL的'select'语句! )


回复讨论(解决方案)

mysqli_query($this -> conn,$this -> sql) 返回的是 mysqli_result 对象,不是 resource (资源)
所以 !is_resource($this->result) 成立,程序提前结束

mysqli_query($this -> conn,$this -> sql) 返回的是 mysqli_result 对象,不是 resource (资源)
所以 !is_resource($this->result) 成立,程序提前结束



要怎么改???

if(!$this -> result || !is_object($this->result)){     
        return $this->output("没有数据,请先执行SQL的'select'语句!");
      }

if(!$this -> result || !is_object($this->result)){     
        return $this->output("没有数据,请先执行SQL的'select'语句!");
      }



终于解决了困扰很多天的问题了,谢谢版主
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
深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

PHP Fatal error: Call to undefined function mysqli_connect()的解决方法PHP Fatal error: Call to undefined function mysqli_connect()的解决方法Jun 23, 2023 am 09:40 AM

在使用PHP编写Web应用程序时,经常会使用MySQL数据库来存储数据。PHP提供了一种与MySQL数据库进行交互的方法,称为MySQLi。然而,有时在使用MySQLi时,会遇到一个错误信息,如以下所示:PHPFatalerror:Calltoundefinedfunctionmysqli_connect()这个错误信息意味着PHP无法找到my

php无法连接mysqli怎么办php无法连接mysqli怎么办Nov 09, 2022 am 10:07 AM

php无法连接mysqli的解决办法:1、打开“php.ini”文件;2、找到“mysqli.reconnect”;3、将“mysqli.reconnect = OFF”改成“mysqli.reconnect = on”即可。

PHP PDO 与 mysqli:比较和对比PHP PDO 与 mysqli:比较和对比Feb 19, 2024 pm 12:24 PM

PDOPDO是一个面向对象的数据库访问抽象层,它为PHP提供了一个统一的接口,允许您使用相同的代码与不同的数据库(如Mysql、postgresql、oracle)进行交互。PDO隐藏了底层数据库连接的复杂性,简化了数据库操作。优缺点优点:统一接口,支持多种数据库简化数据库操作,降低开发难度提供预处理语句,提高安全性支持事务处理缺点:性能可能比原生扩展稍低依赖外部库,可能会增加开销演示代码使用PDO连接mysql数据库:$db=newPDO("mysql:host=localhost;dbnam

mysql的运行文件是什么mysql的运行文件是什么Apr 11, 2023 am 10:38 AM

mysql的运行文件是mysqld;mysqld是一个可执行文件,代表着Mysql服务器程序,执行这个文件可以直接启动一个服务器进程;而mysqld_safe是一个启动脚本,它会间接调用mysqld,并且还会顺带启动一个监控进程。

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服务是否正常运行,如果服务未运行或者启动失败,就可能会导致连接被拒绝的错误。你可

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

在PHP中使用MySQLi建立数据库连接的详尽教程在PHP中使用MySQLi建立数据库连接的详尽教程Jun 04, 2024 pm 01:42 PM

如何在PHP中使用MySQLi建立数据库连接:包含MySQLi扩展(require_once)创建连接函数(functionconnect_to_db)调用连接函数($conn=connect_to_db())执行查询($result=$conn->query())关闭连接($conn->close())

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment