简体   繁体   中英

PHP mysqli() not working

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:

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
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 -a and run the command mysqli_connect(); the response is 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:

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:

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.conf line DirectoryIndex to open certain file names automatically.

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

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

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

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM