php mysqli_connect 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6212858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:40:10  来源:igfitidea点击:

The mysqli_connect not working

phpmysql

提问by Kraken

I installed apache, php and mysql. Now when I execute the php code, the mysqli_connect()is not working, neither it's showing the message in die.

我安装了 apache、php 和 mysql。现在,当我执行 php 代码时,mysqli_connect()它不起作用,也没有在 die 中显示消息。

$dbc=mysqli_connect(' ', ' ', ' ', ' ') or die('not connecting');

Now someone tell me what database username shall I pass to mysqli_connectand what password. I don't remember I was asked for any username or password and why isn't the message in die()showing up?

现在有人告诉我应该传递给哪个数据库用户名mysqli_connect和密码。我不记得有人要求我输入任何用户名或密码,为什么消息没有die()显示?

My var_dump(function_exists('mysqli_connect'));outputs bool(false).. if it has anything to do with it, how do I correct it.?

我的var_dump(function_exists('mysqli_connect'));输出 bool(false).. 如果它与它有任何关系,我该如何纠正它。?

回答by binaryLV

Looks like MySQLi extension is not installed. What does var_dump(function_exists('mysqli_connect'));output?

看起来没有安装 MySQLi 扩展。什么var_dump(function_exists('mysqli_connect'));输出?

回答by dynamic

Do you have error_reporting(E_ALL);And ini_set('display_errors',1);?

你有error_reporting(E_ALL);ini_set('display_errors',1);吗?

The problem could be somewhere else.

问题可能出在其他地方。

Also how do you know it's failing if it is not priting the message in Die()?

另外,如果它没有将消息打印出来,你怎么知道它失败了Die()

回答by Daniel O'Leary

for the host, if you are using "localhost:8889", try using "localhost", and in the mysqli_connect() put in '8889' (or whatever port you are using) as an argument after the other arguements.

对于主机,如果您使用“localhost:8889”,请尝试使用“localhost”,并在 mysqli_connect() 中放入“8889”(或您使用的任何端口)作为其他参数之后的参数。

worked for me.

为我工作。

eg:

例如:

mysqli_connect('localhost','root','root','dbname','8889');

回答by NomanJaved

//1 create a data base connection
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD);
if (!$con) {
    die('mysqli connection failed: ' . mysql_error() );
}
//2 connect with a data base 
$db_select = mysqli_select_db($con , DB_NAME);  
if (!$db_select) {
    die('data base selection failed: ' . mysql_error() );
}
//3 create query
    $result = mysqli_query($con, "select * from subjects");
    if (!$result) {
        die('query not successed: ' . mysql_error() );
    }
//4 use the returned data
    while ($row = mysqli_fetch_array($result)) {
    echo $row['menu_name'] . " " . $row['position'] . "<br>" ;
}
//5 close connection...
if (isset($con)) {      
    mysqli_close($con);
}

Run this code if you have any query then feel free to ask me. 

回答by Mohd Tauovir Khan

First check your php version echo 'Current PHP version: ' . phpversion();exit; if it is above 5.5 and even not working then write script in your php file phpinfo(INFO_MODULES);exit; it show all about php and check Mysqli listed or not. if not then inform to our server admministrator or if you have access then go to phpini file and enable mysqli (remove semicolon from it)

首先检查您的 php 版本 echo 'Current PHP version: ' 。phpversion();退出; 如果它高于 5.5 甚至不工作,那么在你的 php 文件中编写脚本 phpinfo(INFO_MODULES);exit; 它显示了关于 php 的所有信息,并检查是否列出了 Mysqli。如果没有,请通知我们的服务器管理员,或者如果您有访问权限,则转到 phpini 文件并启用 mysqli(从中删除分号)

回答by GolezTrol

If you pass no values, I think MySQL is using defaultsfrom the settings ini. So maybe this happens too if you pass empty values. In that case the connection could actually be established, and the result won't 'die'. Best thing is to use var_dumpto check what $dbccontains after the call and continue from there.

如果您不传递任何值,我认为 MySQL 正在使用设置 ini 中的默认值。因此,如果您传递空值,可能也会发生这种情况。在这种情况下,实际上可以建立连接,并且结果不会“消亡”。最好的办法是在通话后var_dump检查$dbc包含的内容并从那里继续。

But anyway, there is no way, PHP is going to tell you which settings to use if you don't remember them. :)

但无论如何,没有办法,如果您不记得它们,PHP 会告诉您使用哪些设置。:)

回答by Carlos Campderrós

If you just installed mysql, then there exists only the rootuser, without a password (or blank one if you prefer). You are strongly encouraged to change that password ANDcreate a new user and password for your application, who has only access to just one database, the one your application uses.

如果你刚刚安装了 mysql,那么只有root用户存在,没有密码(如果你愿意,也可以是空白的)。我们强烈建议您更改密码创建应用程序,谁才刚刚一个数据库的访问时,一个应用程序使用一个新的用户名和密码。

To change the rootpassword, you may do this:

要更改root密码,您可以这样做:

$ mysql -u root -p
Enter password: [press enter]
mysql> use mysql;
mysql> UPDATE user SET `password`=PASSWORD('your_desired_password') WHERE username='root';

# this next bit is to create a database and a username for your web application
mysql> CREATE DATABASE your_application_name;
mysql> GRANT ALL ON your_application_name.* TO 'your_username'@'localhost' IDENTIFIED BY 'yourpassword';

Obviously change all your_*values with correct ones.

显然,your_*用正确的值更改所有值。

For the reason why the die()gets not executed, do what @yes123 and @binaryLV had said (I think both are right, the mysqliis not installed, so it throws a E_FATAL_ERRORupon calling mysqli_connect(...)and as error_reportingis disabled (or maybe display_errors, or maybe both), you don't see that error.

出于die()未执行的原因,请按照@yes123 和@binaryLV 所说的进行操作(我认为两者都是正确的,mysqli未安装,因此它E_FATAL_ERROR在调用时抛出一个mysqli_connect(...)并且error_reporting已禁用(或者可能display_errors,或者两者),您没有看到那个错误。

回答by C. Silva

Try this:

尝试这个:

$dbc = @ mysql_connect('localhost', 'root', '') or exit('Not connecting.');