DB connect

I made a new web-server with the following.

And i have an old web-server which has apache, PHP, and mySQL
The old one is made by almost one click on Window some years ago.

i can do database Connection with old server.
but
i can’t yet do database connection with the new server.

The following is all code in "correct_oldServer.php

$dbServername="localhost";
$dbUsername="root";
$dbPassword="correct";
$dbConnect = mysql_connect($dbServername, $dbUsername, $dbPassword);

If I open correct_oldServer.php with my browser, it shows nothing.
The following is all code in "wrong_oldServer.php

$dbServername="localhost";
$dbUsername="root";
$dbPassword="wrong";
$dbConnect = mysql_connect($dbServername, $dbUsername, $dbPassword);

If I open wrong_oldServer.php with my browser, it shows the below.

if the dbConnection is correct, it shows nothing.
if the dbConnection is NOT correct, it shows Warning like the above.

Let’s make the same pages on the new server and see the results.

The following is all code in "correct_newServer.php

$dbServername="localhost";
$dbUsername="root";
$dbPassword="correct";
$dbConnect = mysql_connect($dbServername, $dbUsername, $dbPassword);

If I open correct_oldServer.php with my brwoser, it shows nothing.
So far, so good

The following is all code in "wrong_newServer.php

$dbServername="localhost";
$dbUsername="root";
$dbPassword="wrong";
$dbConnect = mysql_connect($dbServername, $dbUsername, $dbPassword);

If I open wrong_newServer.php with my brwoser, it also shows nothing.

Both correct_newServer.php and wrong_newServer.php show nothing means that something is wrong in the db connection.

since the php version of the new server is 7.2,
I also tried mysqli_connect instead of mysql_connect.
but the mysqli trial is failed.

What’s wrong with my new server?
something wrong in the linux, apache, PHP, mySQL, or my PHP code?

How can I make the db connection correctly?

You can’t simply replace mysql with mysqli. Your best bet would be to learn to use PDO. It will take a bit of getting used to but it would be time well spent. YOu can find all you need to know about PDO here: https://phpdelusions.net/pdo

1 Like

That said, the error message given has nothing to do with mysqli.
The fact that it shows ‘nothing’ does not mean that something is wrong in the DB connection, it means your old server had error reporting turned on by default, and the new one doesnt.
At the top of your page, put the following:

<?php
error_reporting(-1);
ini_set('display_errors', '1');
?>

and refresh the page with the bad password.

The below is all code in “errorReport_newServer.php”.

<?php

error_reporting(-1)
ini_set('display_error', '1');

$dbServername='localhost';
$dbUsername='root';
$dbPassword='badPassword;

$dbConnect=mysqli_connect($dbServername, $dbUsername, $dbPassword);
?>

The result of the code above displays nothing(White screen), no error message.
The result of the code below also displays nothing although I expect the error message.

<?php

error_reporting(-1)
ini_set('display_error', '1');

$dbServername='localhost';
$dbUsername='root';
$dbPassword='badPassword';

$dbConnect=mysql_connect($dbServername, $dbUsername, $dbPassword);
?>

How can I connect PHP7.2 to mySQL?

Look verrrrry closely… and then look a second time. There’s two errors in your code.

If you are using PHP 7 then add the following file wide error checking and it should show the errors:

<?php 
declare(strict_types=1);

error_reporting(-1)
ini_set('display_error', '1');

Is this just a typo on this forum, or does your live code have the same typo?

$dbPassword='badPassword;

You have to use mysqli or PDO, you cannot use this line:

$dbConnect=mysql_connect($dbServername, $dbUsername, $dbPassword);

Then, can I use the line below?

The documentation suggests that is the correct syntax. I use PDO myself so I wasn’t sure. You can optionally add the database name as a fourth parameter to save having to separately select it later.

What happens when you try it?

<?php

declare(strict_types=1);

error_reporting(-1);
ini_set('display_errors', '1');

$dbServername='localhost';
$dbUsername ='root';
$dbPassword='correct';
$dbName='test1';

$dbConnect=mysqli_connect($dbServername, $dbUsername, $dbPassword, $test1);

?>

The above is all code in the page.
The below is the browsing result of the above.

Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/index.php:13 

The following shell code shows there surely is the database “test1”.

Since the password is correct, it should produces nothing, ie white page without any errors.
Why does it produce the fatal error?
How can I fix it?

(I didn’t do PDO yet. it seems not easy to understand.)

Stick this in a file and run it:

<?php phpinfo(); ?>

Check the info for a mysqli section.

PHP Version 7.2.19-0ubuntu0.18.04.2

and is there a mysqli section in that page?

I just checked the PHP online manual and a fourth parameter (database name) is required.

https://www.php.net/manual/en/mysqli.construct.php

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 Schlüter

If that’s the only mention of mysqli on the page, your *AMP installation did not include installing the mysqli drivers for PHP; you should repair your installation to include them.

You should see something that contains information (roughly) as follows:

Try this:

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

$dbServername='localhost';
$dbUsername ='root';
$dbPassword='correct';
$dbName='test1';

// THERE IS NO PARAMETER $test1
// $dbConnect=mysqli_connect($dbServername, $dbUsername, $dbPassword, $test1);

// TRY THIS
$dbConnect=mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

// OR THIS
$dbConnect=mysqli_connect($dbServername, $dbUsername, $dbPassword, 'test1');


I am afraid that I was careless.
Although I modified like the below, it still produces the fatal error.

$dbConnect=mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

I need to install the mysqli drivers for PHP?
I need to repair the installation

Can I fix it with your help?
Can I remove the PHP and install it again including mysqli driver for PHP with your help?
Should I reinstall xubuntu, apache, php, mysql? ( I can do it)

How did you install apache/php, is the question.

EDIT: Assuming you used apt-get, what happens when you issue the command sudo apt-get install php-mysql