I'm new to PHP and am trying to write a few PHP scripts which connect to a local MySQL server. FYI, I'm developing on Ubuntu 22.04, coding in PHP 8.1.2, and connecting to a MySQL server, version 8.0.28.
Here's the problem in a nutshell: In my PHP scripts, I try to connect to the DB server using a mysqli object:
$con = new mysqli($servername, $username, $password, $database, $mysqlPort, $socket);
To use mysqli
, you have to install mysqli, which I've done on my development machine. I got the command to work in my first script:
function openDBConn( $servername, $username, $password, $database, $mysqlPort, $socket, $tr )
{
$con = null;
try{
$con = new mysqli($servername, $username, $password, $database, $mysqlPort, $socket);
} catch (Exception $e){
$error = $e->getMessage();
echo $error;
}
print("Connection successful!\n");
// write to DB
$con->close();
}
You'll note that I'm using a function to open a connection, write to, and close the connection to the DB. This bit of code works great.
But now here's my problem: I need a second script to also open a connection to the same DB server. Here's Script #2:
<?php
// DB Variables
$servername = "10.10.10.123";
$database = "myDB";
$username = "user01";
$password = "password01";
$mysqlPort = "3306";
$socket = "/var/run/mysqld/mysqld.sock";
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
echo 'We don\'t have mysqli!!!';
} else {
echo 'Phew we have it!';
}
$con = null;
print("\nOpening db connection...\n");
try{
$con = new mysqli($servername, $username, $password, $database, $mysqlPort, $socket);
} catch (Exception $e){
$error = $e->getMessage();
echo $error;
}
print("Connection successful!\n");
...more...
If you hold both scripts side-by-side, you'll see that I'm creating a new mysqli
object with the same lines of code. The difference is that the second script crashes on the new mysqli()
line. Here's the script output:
We don't have mysqli!!! Opening db connection...
No error is caught, nothing happens after $con = new mysqli()
. The output from the proceeding if()
check does explain the problem: mysqli is not loaded (installed? available? linked?) for this script, which is why Script #2 fails.
But what the heck? I installed mysqli
on this server. The fact that Script #1 works demonstrates this much. Both Script 1 and Script 2 run in the same directory, both are run by the same user, both use the same code. I've searched Script 1 for additional references to mysqli
, but I can't find any. Any idea why one script would have mysqli
and the other wouldn't? The only difference I see between the two is that Script 1 calls $con = new mysqli()
from a function; why would that make a difference?
FYI, my question has sort of been asked on SO before (see here and here) but not with the "one script works, the other doesn't" angle. Any advice/insight is welcome, thank you.
=================================================================== EDIT Based on feedback, I've since created this file in the same directory where Script1 and Script2 are:
me@myUbuntu:/var/www/html$ more SOtest.php
<?php phpinfo();
me@myUbuntu:/var/www/html$
That's it; the entire file is just that one line of code, <?php phpinfo();
. When I set it to the same permissions and ownership as Script1 and Script2 and then run it and scan its output, I see this:
me@myUbuntu:/var/www/html$ sudo php SOtest.php > OUTPUT.txt
me@myUbuntu:/var/www/html$ more OUTPUT.txt | grep mysqli
/etc/php/8.1/cli/conf.d/20-mysqli.ini,
mysqli
mysqli.allow_local_infile => Off => Off
mysqli.allow_persistent => On => On
mysqli.default_host => no value => no value
mysqli.default_port => 3306 => 3306
mysqli.default_pw => no value => no value
mysqli.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock
mysqli.default_user => no value => no value
mysqli.local_infile_directory => no value => no value
mysqli.max_links => Unlimited => Unlimited
mysqli.max_persistent => Unlimited => Unlimited
mysqli.reconnect => Off => Off
mysqli.rollback_on_cached_plink => Off => Off
API Extensions => mysqli,pdo_mysql
me@myUbuntu:/var/www/html$
It sure looks like mysqli is installed, even if Script2 can't see it.
=================================================================== EDIT Some commenters asked how Script1 and Script2 are called. I originally thought that both Script1 and Script2 were called by the website's HTML code. But that's not actually true. Script1 is actually called by another PHP script, a wrapper script that is invoked by the HTML. Here's the bit of PHP that calls Script1:
function runDBOperations(){
print("Called runDBOperations()...");
$someOutput = shell_exec("php /var/www/html/Script1.php");
print("...done!\n");
}
I'd forgotten that the wrapper PHP script invokes Script1 in a function. Interesting.
Script2, however, is called directly from the HTML code:
<html>
<body>
<form action="Script2.php" method="post">
Search <input type="text" name="search"><br>
<input type ="submit">
</form>
</body>
</html>
(Yes, that's the entire webpage at this point.)
...There must be something in that wrapper PHP script which loads mysqli
, don't you think? HmmmmMMMMmmmmm....