Please help - Following a video tutorial, script not working

Hello! I’ve just started learning PHP a few days ago. I wanted to learn PHP so I could make a browser game - similar to this site called VirtuRatz. I’ve been determined and really trying my best, watching hours of PHP tutorials, but it wasn’t until I found a certain Youtuber that makes videos on PHP coding that I was able to understand. He explains it in a way that I can really learn and understand. So anyways, I’ve been following his tutorial, and a script he makes on the second video has thrown me off. I can’t get it to work. It’s the script he uses to connect the Mysqli database to the config.php… it is found about 15 minutes into this video:

https://m.youtube.com/#/watch?v=opNnomhxcQs

Basically, when he runs it, it will tell him on his screen that it has successfully connected, but it does nothing for me, and it’s an important script. If I can’t get it to work, I’m not sure what to do.

If you could help me, I would GREATLY appreciate it. I’ve tried different forums, but nobody seems to understand I’m a newbie. This forum seems like a nicer community, so I hope someone here can help. Thank you so much!

You need to determine what your code is actually doing when you run it on your server.

Start by posting your actual code, less any database connection credentials, tell us or show us what url you used when you requested your code (what does the address bar in your browser show), and show us or tell us what result you got, and if you got a blank page, what does the ‘view source’ of that page show?

Also, is php’s error_reporting set to E_ALL and display_errors set to ON, in the php.ini on your development system, so that php would help you by reporting and displaying all the errors it detects?

Now the getPage function in his 3rd tutorial doesn’t work either :confused:
I get a fatal error for that one… Call to undefined function getPage()

But back on the subject of my original post, here’s my code, it should be almost exactly like his, except the name of my project is Radical Rodents:
[php]<?php
require_once(“system/functions.php”);
require_once(“system/config.php”);

//DATABASE CONNECTION
$dbserver = “localhost”;
$dbusername = “root”;
$dbpassword = " ";
$db = “radical rodents”;

//CREATE CONNECTION
$conn = new mysqli($dbserver, $dbusername, $dbpassword);

//CHECK CONNECTION
if($conn->connect_error)
{
die(“Connection failed”.$conn->connect_error);
}
else
{
//IF CONNECTION IS GOOD, GET DATA FROM DATABASE
$query = “SELECT name, seperator, description, maintenance, logo FROM configuration”;
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$name = $row [‘name’];
}

//TECHNICAL SETTINGS
$maintenance = false;

?>[/php]

Thank you for replying so quickly! I am on the localhost in Chrome, and I have my project open on the “index.php” file.

Oh, by the way, I went ahead and started getting a few things done on his 3rd video, FYI. That’s why the script doesn’t just echo that it was successful anymore. However, when I did have it set to echo that the connection was successful, it didn’t work.

Hi,

Sort of guess work you’re expecting us to do for you…

Your script requires two other scripts (functions.php as well as config.php)are these ready?
DId you try to do what phdr advised you to do?

Perhaps this may help you to start from basics: https://www.w3schools.com/php/default.asp

I’m sorry, but I looked for the setting phdr mentioned, and wasn’t able to find it. I am getting several PHP basics books from the library soon in hopes it will help me… Hopefully I am able to understand them. What I don’t get is why it worked for him, but not me? I did it exactly how the person who made the video said. Thanks for your help, and I will check out that link too, Cluster_One! Thank you! :slight_smile:

Edit: Also, sorry I didn’t realize I hadn’t answered your question. What do you mean by “ready”? They are there, and they have at least one script, yes.

Hello,

That youtube link takes me to youtube main page, not to the tutorial :slight_smile:

These settings (E_ALL etc) can be found in php.ini (i think, has been some time since i played with it.) Also, not sure if you’re on Windows or Linux.
If you decide to change settings in php.ini make sure you back this file first (i mean php.ini one).

Your main script requires those two scripts/files to be accessible to the main script. That looks to me that these should be located in folder ,system, which is in the same folder as where you run your main script from.If these happen to be located somewhere else, your main script will not find them and therefore possibly will not run.

Not saying that this is the culprit but one can’t know…You will definitely find that error reporting useful, so put your effort into figuring out how to make sure it is working on your system.
Do not worry google it (how to set up error reporting in php etc…) as it will be likely the same or similar…

So far, the only indication that any of your php code is running at all, is the fatal runtime error you mentioned in reply #2, which is for some code other than the current problem code.

Back to the php error_reporting/display_errors settings. How have you obtained and installed your web server/php? If you used one of the all-in-once packages, the control panel for it should have a way of editing the php.ini. Otherwise, you can find where the php.ini that php is using is, by creating a .php script file with the following in it, then request the file through your web server using your browser -
[php]<?php
phpinfo();
?>[/php]

There will be a Loaded Configuration File line in the resulting output that shows the path and filename of the php.ini that you would need to edit with the error_reporting/display_errors settings. The output from the phpinfo() statement also shows what the error_reporting/display_errors settings are. To insure that any changes you make to the php.ini take effect, you need to restart your web server and check using a phpinfo() statement that the changes actually have taken effect.

While you are making changes to the php.ini, find what the output_buffering setting is, and set it to OFF, it if is anything other than OFF. This setting hides problems and hides output from your code and php errors, making it harder to write and test code. The only time you should use output buffering is if you need to buffer output and this setting should normally be off.

This should get you to the point where php and/or your code will give you an indication of why it isn’t running on your server. As to why the code you are finding in a tutorial doesn’t work for you, there can be typos, differences in php configuration, and differences in php versions that would allow code to work on one server but not another. This is why you must get php to help you when you are leaning, developing, and debugging code, by having the error_reporting/display_errors settings set as suggested.

You also need to ALWAYS have error handling for statements that can fail, in this case the database connection and query statements, and you shouldn’t output the actual error messages when running code on a live/public server, as this gives hackers useful information. The easiest way of adding error handling, that doesn’t involve adding conditional logic for each statement (Keep It Simple - KISS), is to use exceptions and let php catch any exceptions. This also addresses not outputting the actual error information when on a live serve, since php will use the error_reporting/display_errors/log_errors settings to control what happens with the actual error information. When on a live server, you would set display_errors to OFF and set log_errors to ON. To enable exceptions for the mysqli statements, add the following two lines of code before the database connection code, and remove the code trying to detect the connection error (which may not be working correctly anyway) -
[php]
// note: the variable $driver in these two lines of code can be any chosen name and is not tied to any variable name you are using in the rest of your database code
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; // MYSQLI_REPORT_ALL <- w/index checking; w/o index checking -> MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;[/php]

BTW - the code in reply #2 is not selecting a database, so after you get the error_reporting/display_errors settings set as suggested and enable exceptions, you should be getting some useful error output when the code runs. Also, the $conn->connect_error doesn’t work in older php versions and the posted code may not be detecting a connection error at all, which may account for the symptoms you are getting, but this problem will go away if you switch to use exceptions and remove the code trying to use $conn->connect_error. What php version are you using?

Lastly, the php mysqli extension is the not written the best (the problem with the $conn->connect_error not working in some versions of php is just one of the many problems with it.) You should instead learn the php PDO extension. It is better written and is simpler and more straight-forward to use.

Sponsor our Newsletter | Privacy Policy | Terms of Service