Need Assistance Out of Date PHP Code

In 2012 I studied Web Development at TAFE College. We created a Website using PHP MySQL and HTML & CSS of course. My site no longer works in the newer PHP environment (For want of better words) - The Newer Version of PHP does not recognise the older PHP code. Is there a way to find out what the newer code is to replace the older code (Without having to spend big bucks going back to TAFE of course)?

Yes! Post your questions here and we will help! ( This is why this site exists! )
AND, Welcome back to the site!

With that said… There are two major areas you will need to upgrade. The MySQL should be updated to PDO. Easy to learn and not a big deal to upgrade. The other are older commands no longer valid.
These will show up in you PHP error logs and you can fix them one by one. Where would you like to start?

Set up your code on a development server with all errors displayed, and try running it. You’ll probably see a bunch of stuff; the last error shown on your page will be the one that has stopped your page running. The PHP site has excellent pages describing the differences between each PHP version, that plus your error log will help you get your code up to scratch.

This is not necessarily true. You could use MySQLi

I’d do it in a virtual environment, perhaps you can use Laravel/Homestead for this.

Another thing I’d recommend is to use XDebug + an IDE (PhpStorm if possible, otherwise Visual Studio Code will do).

Let me know if you need further assistance

Well, MySQLi is about the same to learn as PDO. PDO works with 12 databases. MySQLi works with 1.
If another database is used in the future, then PDO is the way to go. Otherwise either will work. Sometimes I forget to say the reason why I suggest a library.

And, this is a beginner. I would not confuse him with Laravel/Homestead. Why add more to learn right off at the start. Use PhpStorm if you can afford it or just NetBeans which is free and easy to learn. Visual Studio is okay too, but, not really PHP orientated.

Keith, please note that every programmer likes his/her favorites. You need to pick your own and find ones that fits your personal needs.

Ok So I Use Xampp as the “server”, which is what we were instructed to do.
When type in the “URL” I get this error message:
Fatal error : Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\ComputerSuperStore\header2.php:30 Stack trace: #0 C:\xampp\htdocs\ComputerSuperStore\index.php(3): include() #1 {main} thrown in C:\xampp\htdocs\ComputerSuperStore\header2.php on line 30

I Created the site specifically for the TAFE Course, it is not an actual site, but, at the time it would have worked fine. The mysql_connect did connect to the database, the include did work which obviously is outdated “language/ script” I have tried a few newer ideas, non of which have worked.
I hope this is specific enough, that said, I am guessing that once I find the fix for those points, that the rest of the script may cause an error as well.

As per my reply to ErnieAlex the “local server” we were instructed to use was Xampp. I know that there are probably others around, some of which cost the big bucks, and are a lot of messing around to get up and running. I loaded the “site” into the appropriate folder, then typed the URL and this is the error it returned:

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\ComputerSuperStore\header2.php:30 Stack trace: #0 C:\xampp\htdocs\ComputerSuperStore\index.php(3): include() #1 {main} thrown in C:\xampp\htdocs\ComputerSuperStore\header2.php on line 30

I pretty much get what it is saying, and my interpretation is that it no longer accepts “mysql_connect” nor does it accept the “include” command. I have tried a couple of different “replacement” commands none of which worked. I also assume that as all the php was written in 2012, probably most of the commands will no longer work either. hence why I am seeking assistance… I don’t have a lot of spare cash as I am on disability, and only trying to get my skills/ knowledge up to date so that I may be able to put it to use further down the track.

The reason this is undefined is because the mysql_ extension has been completely removed from php, which was known at the time you took this course.

Since you have to go through all the database statements to update them, and since some security for string data was also removed from php, updating to the much simpler and more consistent PDO extension and using prepared queries when supplying external, unknown, dynamic values to the query when it gets executed will both reduced or eliminate the amount of changes you have to make, will provide security for all data types, and will future proof you code.

I see, so, all my learning in PHP - MySQL is totally useless… and I am assuming PDO is a whole new language, and is totally different from the previous? Does PDO work in the local server XAMPP? I fear I am getting out of my league here… There are a lot of “queries” to display certain parts of the database. The Site I developed was a “Pretend” computer store where a customer could create an account, log in, online search/ shop for computers, Laptops, etc. All the queries were used with mySQL.

There’s nothing in that error that has anything to do with the include statement not working. BTW - You should actually use ‘require’, not include, for things that your code must have for it to work.

Provided you haven’t used any deprecated, now removed, php features in your code, there’s actually very few changes to php that would affect a beginner/basic web site.

Well, MySQL is not longer. Therefore, you will need to look at all of your files and find every reference to it and change it to MySQLi. That is step one. Now, when you upgrade from MySQL to MySQLi, you have to make some minor changes. In MySQLi, you have a reference back to the connection. Here is a site that discusses MySQLi (Object orientated and procedural) and PDO. I would suggest using MySQLi “procedural” as it is easier for a beginner to learn than using data-objects. Later on, OO is better, but, to start with not.

Yes, Xampp just fine! This is a simple server/PHP/database emulator and is perfect for testing and learning! ( And, FREE! ) And, no, learning all the MySQL is not a waste of time. The newer MySQLi just needs some minor changes to get it working again.

So, change all your references to MySQL into the MySQLi versions and post your problems. We can help with any you run into.

As it was developed back i 2012, I assume that there would be a lot of deprecated/ now removed, php features in my code.

No. PDO is a php extension that provides a set of statements to use for accessing a database. Please see the php.net documentation - https://www.php.net/manual/en/book.pdo.php

The MySql database server itself is still the same. The basic sql query syntax remains the same, though converting to a prepared query requires removing variables from the sql statement, replacing them with ? place-holders, then supplying the removed variables as an array to an ->execute([…]) statement.

so pardon my lack of knowledge, but changing from mysql to mysli, do I just add the “i” to all the commands that have mysql, or are the newer commands that replace… i am afraid i am lost here.

The migration sections in the php.net documentation list the major, backward incompatible, changes you would need to consider - https://www.php.net/manual/en/appendices.php

Nope. There are calling parameter differences and some mysql statements don’t have equivalent mysqli statements. You will need to use the php.net documentation to find the proper use.

As was already stated, since you have to go through a learning exercise, you might as well skip the limited mysqli extension, and just learn how to use the PDO extension. Several of the active forum members would be more then willing to post PDO examples based on what you are currently doing in your code.

Ok I have actually made a “little progress” BUT, as I suspected, there are more errors/ warnings. While some of the page has actually loaded, it is not loading anything in the actual data base.

  1. Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\ComputerSuperStore\header2.php on line 34

  2. Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\ComputerSuperStore\index.php on line 23

  3. Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\ComputerSuperStore\index.php on line 23

  4. Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\ComputerSuperStore\index.php on line 30

They are the warnings, I have the code open in Visual Studio at the moment.

Well, in most cases, you need to add the connection to all your SELECT’s, FETCHES and QUERIES.
So, things like mysqli_select_db() would require the connection info first parameter. Query(), too… etc…
You create a connection and assign it to let’s say, $con… Then, mysqli_query($con, rest of line)
Basically, make the first parm a pointer back to the connection string. Make sense?

For beginners, the link I posted gives you all the examples for all of your function calls.

Yes I have been reading through the link you provided
And the use of $con is there in just about every case. Line 34 and line 35 from header2.php:

mysqli_select_db(“computersuperstore” ,$con);
require (‘topbar.php’);

Which is the first “warning”…

I note that mine reads (“computersuperstore” , $con), should it read ($con, “computersuperstore”) now?

$con is the FIRST parrm, before computersuperstore…

Sponsor our Newsletter | Privacy Policy | Terms of Service