回音mysqli_connect_errno不起作用 [英] Echoing mysqli_connect_errno not working
问题描述
开始学习PHP,然后学习了基本知识,这很容易,因为我在学校学习C ++,而且大多数东西都相似,所以我从mysql开始.一切正常,但connect_errno
无法正常工作.
复制并粘贴此 http://php.net/manual/zh/mysqli.connect-errno.php 进入我的代码,没有成功.
我的几行代码:
<?php
error_reporting(0);
$db = mysqli_connect('127.0.0.1' , 'root' , '' , 'mydb');
if ($db->connect_errno) {
die('Connect Error: ' . $db->connect_errno);
}
?>
我还尝试了教程中的示例,但没有成功.
答案在下面!
您混合了面向对象方法和过程方法.以下是面向对象方法的示例.
$db = new mysqli('127.0.0.1' , 'root' , '' , 'mydb');
if ( $db->connect_errno ) {
die('Connect Error: ' . $db->connect_errno);
}
error_reporting( 0 );
将消除错误,因此,如果您在调试工作流程中并需要查看这些错误,则需要将错误报告指令更新为error_reporting( E_ALL );
. >
此外,我将代码名更新为mydb
,如代码所示(我在本地计算机上将其切换为尝试连接/错误).
Started learning PHP and after learning the basic stuff, which was easy because I learn C++ at school and most of the stuff is similar, I started with mysql. Everything works fine, but I can't get to work the connect_errno
working.
Copied and pasted this http://php.net/manual/en/mysqli.connect-errno.php into my code with no success.
My few lines of code:
<?php
error_reporting(0);
$db = mysqli_connect('127.0.0.1' , 'root' , '' , 'mydb');
if ($db->connect_errno) {
die('Connect Error: ' . $db->connect_errno);
}
?>
I've also tried an example from a tutorial, no success.
EDIT: ANSWER IS BELOW!
You mixed the Object-oriented approach and the Procedural approach. Below is the example for the OO approach.
$db = new mysqli('127.0.0.1' , 'root' , '' , 'mydb');
if ( $db->connect_errno ) {
die('Connect Error: ' . $db->connect_errno);
}
error_reporting( 0 );
will suppress errors, so if you're in a debugging workflow and need to see those errors, you'll need to update the error reporting directive to error_reporting( E_ALL );
.
Also, I updated the db name to mydb
as reflected in the code (I switched that out on my local machine to experiment with connections/errors).
这篇关于回音mysqli_connect_errno不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!