PHP mysqli() 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21972038/
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 04:35:30  来源:igfitidea点击:

PHP mysqli() not working

phpmysqlapacheubuntumysqli

提问by austinheiman

To start, I am new to PHP, and this is my first post on stackoverflow. I am trying to use mysqli_connect() to write a record to a users table in a test database. In mysql-client I can add a record with the command:

首先,我是 PHP 新手,这是我在 stackoverflow 上的第一篇文章。我正在尝试使用 mysqli_connect() 将记录写入测试数据库中的用户表。在 mysql-client 中,我可以使用以下命令添加记录:

INSERT INTO users VALUES ( 'id' , 'smith' , 'joe' , 'joepass' );

INSERT INTO users VALUES ( 'id' , 'smith' , 'joe' , 'joepass' );

As far as I can understand, the following .php page should add joe smith to the users table when it is hit in a browser:

据我所知,以下 .php 页面在浏览器中被点击时应该将 joe smith 添加到用户表中:

<?php
echo "php start";

$con = mysqli_connect( "localhost" , "root" , "rootpass" , "db_name");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query( $con , "INSERT INTO users values ( 'id' , 'smith' , 'joe' , 'joepass' )");

mysqli_close($con);

echo "php end";
?>

But when I go to this page in a browser, all I see is the text "php start" which is the first echo statement. I would expect to see the words "php end" as well. When I check the users table in mysql client I see no new record. Also when I open the php interactive shell with php -aand run the command mysqli_connect();the response is PHP Fatal error: Call to undefined function mysql_connect() in php shell code on line 1.

但是当我在浏览器中访问此页面时,我看到的只是文本“php start”,这是第一个 echo 语句。我也希望看到“php end”这个词。当我检查 mysql 客户端中的用户表时,我看不到新记录。此外,当我打开 php 交互式 shellphp -a并运行命令时mysqli_connect();,响应是PHP Fatal error: Call to undefined function mysql_connect() in php shell code on line 1.

This leads me to believe that mysqli_connect is not installed or configured properly in php.ini, but I dont have a good enough understanding of php.ini to troubleshoot very well. Also, I have a phpinfo() test.php page. In the "Credits" table of phpinfo() I see the following lines:

这让我相信 mysqli_connect 没有在 php.ini 中正确安装或配置,但我对 php.ini 的理解不够好,无法很好地排除故障。另外,我有一个 phpinfo() test.php 页面。在 phpinfo() 的“信用”表中,我看到以下几行:

MySQL driver for PDO    George Schlossnagle, Wez Furlong, Ilia Alshanetsky, Johannes Schlueter
MySQLi    Zak Greant, Georg Richter, Andrey Hristov, Ulf Wendel
MySQLnd    Andrey Hristov, Ulf Wendel, Georg Richter, Johannes Schlueter
MySQL    Zeev Suraski, Zak Greant, Georg Richter, Andrey Hristov

To install php, apache, and mysql on this Ubuntu server, the commands I have run are:

要在这台 Ubuntu 服务器上安装 php、apache 和 mysql,我运行的命令是:

sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install php5-cli

and the only additional configuration I have done that I can remember is adding a couple of file names to the apache2/mods-available/dir.confline DirectoryIndexto open certain file names automatically.

我记得的唯一额外配置是apache2/mods-available/dir.conf在行中添加几个文件名以DirectoryIndex自动打开某些文件名。

Is there any mysqliconfiguration that needs to be done before the functions can be used?

mysqli功能使用前是否需要进行任何配置?

Also, this is an AWS server, but I dont see how there could be any networking problem to 'localhost'... :P

此外,这是一个 AWS 服务器,但我不知道“本地主机”怎么会有任何网络问题......:P

I will also include a grep -r 'mysqli'output in the /etc/php5directory:

我还将grep -r 'mysqli'/etc/php5目录中包含一个输出:

php5 grep -r 'mysqli'
apache2/php.ini:; http://php.net/mysqli.max-persistent
apache2/php.ini:mysqli.max_persistent = -1
apache2/php.ini:; http://php.net/mysqli.allow_local_infile
apache2/php.ini:;mysqli.allow_local_infile = On
apache2/php.ini:; http://php.net/mysqli.allow-persistent
apache2/php.ini:mysqli.allow_persistent = On
apache2/php.ini:; http://php.net/mysqli.max-links
apache2/php.ini:mysqli.max_links = -1
apache2/php.ini:; http://php.net/mysqli.cache_size
apache2/php.ini:mysqli.cache_size = 2000
apache2/php.ini:; Default port number for mysqli_connect().  If unset, mysqli_connect() will use
apache2/php.ini:; http://php.net/mysqli.default-port
apache2/php.ini:mysqli.default_port = 3306
apache2/php.ini:; http://php.net/mysqli.default-socket
apache2/php.ini:mysqli.default_socket =
apache2/php.ini:; http://php.net/mysqli.default-host
apache2/php.ini:mysqli.default_host =
apache2/php.ini:; http://php.net/mysqli.default-user
apache2/php.ini:mysqli.default_user =
apache2/php.ini:; Default password for mysqli_connect() (doesn't apply in safe mode).
apache2/php.ini:; *Any* user with PHP access can run 'echo get_cfg_var("mysqli.default_pw")
apache2/php.ini:; http://php.net/mysqli.default-pw
apache2/php.ini:mysqli.default_pw =
apache2/php.ini:mysqli.reconnect = Off
cli/php.ini:; http://php.net/mysqli.max-persistent
cli/php.ini:mysqli.max_persistent = -1
cli/php.ini:; http://php.net/mysqli.allow_local_infile
cli/php.ini:;mysqli.allow_local_infile = On
cli/php.ini:; http://php.net/mysqli.allow-persistent
cli/php.ini:mysqli.allow_persistent = On
cli/php.ini:; http://php.net/mysqli.max-links
cli/php.ini:mysqli.max_links = -1
cli/php.ini:; http://php.net/mysqli.cache_size
cli/php.ini:mysqli.cache_size = 2000
cli/php.ini:; Default port number for mysqli_connect().  If unset, mysqli_connect() will use
cli/php.ini:; http://php.net/mysqli.default-port
cli/php.ini:mysqli.default_port = 3306
cli/php.ini:; http://php.net/mysqli.default-socket
cli/php.ini:mysqli.default_socket =
cli/php.ini:; http://php.net/mysqli.default-host
cli/php.ini:mysqli.default_host =
cli/php.ini:; http://php.net/mysqli.default-user
cli/php.ini:mysqli.default_user =
cli/php.ini:; Default password for mysqli_connect() (doesn't apply in safe mode).
cli/php.ini:; *Any* user with PHP access can run 'echo get_cfg_var("mysqli.default_pw")
cli/php.ini:; http://php.net/mysqli.default-pw
cli/php.ini:mysqli.default_pw =
cli/php.ini:mysqli.reconnect = Off

回答by miken32

How about the mysqli section of phpinfo()? Is there one? I suspect you need to install the php5-mysql packgage. I see this when that package is installed:

的 mysqli 部分怎么样phpinfo()?有吗?我怀疑您需要安装 php5-mysql 包。安装该软件包时,我看到了这一点:

~$ cat /etc/php5/conf.d/mysqli.ini 
; configuration for php MySQL module
extension=mysqli.so