Connecting to MySQL DB Confirmation

#1

I am simply connecting to the MySQL database but for the life of me I cannot see or find any proof that the connection succeeded!

In my index.html I have: <?php include 'connect.php';?>

Here is the connect.php:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$db = "testdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $db);

// Check connection
if ($conn->connect_error) {
    // die("Connection failed: " . $conn->connect_error);
trigger_error("Could not connect!",E_USER_ERROR);
echo "<h2>Connection Failed!</h2>";
}
   echo "<h2>Connected successfully</h2>";

?>

I’m not seeing anything about the connection! I’m taking it a step at a time. First, connect and verify connection. Then I will actually fetch or store something in the DB.

I’ve looked in the apache2 and mysql logs. Nothing.

I am new to php so I’m probably doing something wrong?

Thanks

Ray

#2

By default, php code is NOT parsed and interpreted in .html files. Use a .php extension for any file that contains php code.

Next, use ‘require’ for things you code needs for it to work. This will halt execution if the file/path is not found, which will prevent follow-on errors.

Use exceptions to handled database statement errors (connection, query, prepare, and execute) and in most cases let php catch and handle the exception where it will use its error related settings to control what happens with the actual error information (database errors will get displayed or logged the same as php errors.) The exception to this rule is when you need to detect the insertion/update of duplicate user submitted data. In this case, your code should catch the exception, detect if a duplicate error occurred, and setup a useful error message. Using exceptions for errors will let you eliminate all the error handling logic you have now.

You also need to use the PDO extension instead of the msyqli extension. The PDO extension is simpler (takes fewer lines of code to accomplish most tasks) and is more consistent (the result form prepared and non-prepared queries can be treated the same.)

Lastly, do you have php’s error_reporting set to E_ALL and display_errors set to ON, in the php.ini on your system, so that php will help you by reporting and displaying all the errors it detects?

#3

Thanks phdr.

Firstly, I changed the index.html to index.php. Still nothing in the apache2 or mysql error logs. I could not find a specific error log for php so I changed php.ini:

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_log = /var/log/php_errors.log

I don’t see php_errors.log file at all? I would expect to see it and be empty at the least?

I’ll have to see how to use PDO.

Ray

#4

What do you see in the browser, and if it is a blank page, what does the ‘view source’ of the page show?

What URL you are using in your browser’s address bar?

Depending on how php is integrated with the web server, you may have to stop/start your web server to get any changes to php.ini to take effect. You will also need to set the log_errors setting to ON. You should confirm that any changes you make to the php.ini have taken effect by using a phpinfo() statement in case the php.ini that you are changing is not the one that php is using.

Has any php code produced output?

#5

I see what I expect to see in the browser…I am showing a live streaming video. That is working as expected. However I want to store some information about the video into the database, specifically the broadcast streamID. That is my immediate goal. Once I can do that, then I’ll add additional code.
But first I need to be sure I am connecting to the DB.

The view source is showing everything that is in the index.php file. I’d rather not reveal the URL, as I don’t want anyone else to access. I can give it to you privately if you want?

I restarted my application as well as apache2.

Ray

#6

…and log_errors is ON

#7

If by that, you mean you see the raw php code, it means that php is not installed on your server. If you are seeing something else, you need to be specific about what it is, because we are not there with you and don’t know what you are seeing, or what you expect to see.

Also, you should be learning, developing code, and debugging code/queries on a localhost development system, not on a live/public server.

#8

php -v
PHP 7.2.19-0ubuntu0.18.04.1 (cli) (built: Jun 4 2019 14:48:12) ( NTS )
Copyright © 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright © 1998-2018 Zend Technologies
with Zend OPcache v7.2.19-0ubuntu0.18.04.1, Copyright © 1999-2018, by Zend Technologies

I understand about dev versus live/public. Firstly I am not a developer. I am more-less dabbling in this effort. I was having some major issues using a local dev (Vbox VM), so I decided to move to a VPS.

I only have that one line of php code: <?php include 'connect.php';?>

I tried to insert the <?php and ?> at the top and bottom of the index.php file, but for the ?> part, the > was highlighted in red? So I am not sure what that means?

#9

So, I’m trying to run an example of ajax I found here: https://openenergymonitor.org/forum-archive/node/107.html

It does not work for me, meaning I never see the output from api.php. Although the comments left there praise the scripts? I copied and pasted the two .php files as well as created the database.
Any idea what I am missing?

Thanks,

Ray

#10

That’s the php command line interpreter (CLI.) All that means is (part) of php is present on your system, you navigated to a folder, and executed a statement through the operating system. That doesn’t have anything to do with the web server and web pages.

Highlighted in red where? If that means in the ‘view source’ in the browser, php code won’t be seen in the browser since php is a server-side scripting language. It’s purpose is to do things on the server when the page gets requested. The only thing you will see in the browser from php code is the output that the php produces and sends to the browser when it runs.

That script is way out of date. The mysql_ extension in the php code won’t run at all. The html markup is old and the jquery version is out of date, though the JavaScript code making use of the jquery will probably work.

Exactly how did you create the database?