Php 新的Mysqli对象为Null
我正在尝试使用PHP中的Mysqli创建到MySQL数据库的连接。当我在独立页面上执行以下代码时:Php 新的Mysqli对象为Null,php,mysql,mysqli,Php,Mysql,Mysqli,我正在尝试使用PHP中的Mysqli创建到MySQL数据库的连接。当我在独立页面上执行以下代码时: <?php ini_set('display_errors', 'On'); error_reporting(E_ALL); $link = new mysqli('localhost', 'myuser', 'mypass', 'dbname'); var_dump($link); 我也遇到了这个问题,试着调试它的时候简直疯了。事实证明,有时候不管出于什么原因,mysqli对象都不会被
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$link = new mysqli('localhost', 'myuser', 'mypass', 'dbname');
var_dump($link);
我也遇到了这个问题,试着调试它的时候简直疯了。事实证明,有时候不管出于什么原因,mysqli对象都不会被填充,但是直接访问它的属性仍然会执行它背后的本机代码。因此,即使整个mysqli对象的var_转储显示空属性,如果您单独访问它们,它们仍然存在。如果errorno结果为false,则可能执行了一个有效的查询,结果集为空,这是您不希望看到的。希望这有帮助
$mysqli = mysqli_connect('localhost', 'root', '', 'test', 3306);
var_dump($mysqli);
var_dump($mysqli->client_info);
var_dump($mysqli->client_version);
var_dump($mysqli->info);
以及输出:
object(mysqli)[1]
public 'affected_rows' => null
public 'client_info' => null
public 'client_version' => null
public 'connect_errno' => null
public 'connect_error' => null
public 'errno' => null
public 'error' => null
public 'field_count' => null
public 'host_info' => null
public 'info' => null
public 'insert_id' => null
public 'server_info' => null
public 'server_version' => null
public 'stat' => null
public 'sqlstate' => null
public 'protocol_version' => null
public 'thread_id' => null
public 'warning_count' => null
string 'mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $' (length=50)
int 50008
null
int 0
string 'localhost via TCP/IP' (length=20)
string '5.5.20-log' (length=10)
int 50520
我知道这有点老了,但是对于那些仍然经常出现这个问题的人来说,从user3158900到danperron的帖子的评论有解决方案。我在这里发帖是为了让它更显眼。此外,还包括一些示例代码:
global $mysqli; //assuming you've already created your $mysqli object
$mysqli->query("INSERT INTO users SET username='jim' and password='somehash'");
var_dump( $mysqli ); //prints the full object but all the values are null
//the following code prints the full object with appropriate values such as
//insert_id, affected_rows, etc
//view http://pastebin.com/Mgd2CZsM for an example of this output
echo "<pre>" . print_r( $mysqli, true ). "</pre>";
global$mysqli//假设您已经创建了$mysqli对象
$mysqli->query(“插入用户设置用户名='jim'和密码='somehash'”);
var_dump($mysqli)//打印完整对象,但所有值均为空
//以下代码使用适当的值打印完整对象,例如
//插入\u id、受影响的\u行等
//看法http://pastebin.com/Mgd2CZsM 有关此输出的示例,请参见
“回声”。打印(mysqli,true)。"";
试试$link=newmysqli('localhost','myuser','mypass','dbname')或die(mysqli_error())
@crush:我想你的意思是$link=newmysqli('localhost','myuser','mypass','dbname')或die(mysqli\u connect\u error())代码>。mysqli\u error()
函数需要一个链接标识符作为其参数。@Justin,我将其更改为您建议的值,但根本没有更改。实例化mysqli对象后,mysql\u connect\u error函数返回null,假设没有错误。尽管mysqli对象的属性都是空的。当我转储$link时,我得到:尝试通过命令行PHP解释器运行您的独立页面,并用输出修改您的问题。var_dump是这样的:刚才也遇到了这个问题,您的解决方案起了作用,但我发现print_r
也可以适当地打印出对象。