DB connection in php v.7 as compared to v.5.6

A week or so ago I switched my php version from 5.6 to 7. I’ve now discovered that my forms no longer work because they can’t connect to the database. I switched back to 5.6 and they work again.

I’ve done a google search to try to figure out what v.7 requires which is different from v.5.6 but have come up with nothing.

Could someone please tell me what I need to change in the call to the DB in order to use v.7 instead of 5.6? My current call is:

<?php require_once('Connections/borzoiclubdb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }


The page was throwing up this error:

Fatal error : Uncaught Error: Call to undefined function mysql_pconnect() in /raid/home/borzoiclub/borzoiclubofamerica/Connections/borzoiclubdb.php:9 Stack trace: #0 /raid/home/borzoiclub/borzoiclubofamerica/NewTitleAdForm.php(1): require_once() #1 {main} thrown in /raid/home/borzoiclub/borzoiclubofamerica/Connections/borzoiclubdb.php on line 9

Line 9 is the last (blank) line in the code block above but I’m pretty sure the problem is with the connection part, as one of my insert.php files for another form is throwing up the fatal error for line 2, which is:

$con = mysql_connect("","borzoiclubdb","mypassword");

Could it be because these old files are using mysql instead of mysqli? That just occurred to me as I was copying the code. Will v.7 only work with mysqli, not with mysql?

These functions were removed from PHP 7.

Use MySQLi instead, or switch directly to PDO (recommended).

1 Like

Thanks chorn (and also gandalf458). Trying to figure out PDO is on my list of things to do. I guess I’d better move it higher up the list.

Yes that’s critical, but also gives you the opportunity to implement Prepared Statements (if you haven’t already). Then you also get rid of a lot of obsolete code like the above.

Am trying to get my aging brain around Prepared Statements too. It’s a slow process, mainly because I can’t remember anything for longer than about 12 seconds, so have to keep going back and back and re-reading until it finally registers. :slight_smile: Thanks chorn.

You might find this site helpful in learning PDO @Helen_W_Lee.

2 Likes

THANK YOU @gandalf458!! (Thanks also for showing me how one tags a person here. I’ve been trying to figure it out without success. :slight_smile: )

1 Like

It is possible to test to see what PHP version is being used.

https://www.php.net/manual/en/function.phpversion.php

I would be tempted to test for the PHP version in use and include/require the relevant file.

Having two files will eliminate the possibility of making changes to the single file and possibly resulting in the old version from working.

Thanks @John_Betong. I do know how to check which version I’m using and my server guys sent me a line of code to include in my .htaccess file which switched me over to using v.7xx but that was what caused my form(s) to break. I’ve removed that line of code from the .htaccess file now, so that I’m back to v5.6 and the forms work.

I’m not sure I’m understanding what you mean about two files, though. Could you please explain further?

I’m working my way through that site you pointed me to and I think I’m following what it says. I guess the only way I’ll know for sure is to jump in and try it out. Hopefully that doesn’t result in me having to ask a bunch more questions here. :slight_smile:

Thank you SO much for sending that link! I’ve been using w3schools.com for tutorials on this stuff but it doesn’t really address PDO, so your suggestion will be a big help.

Test to see what PHP version is being used and include the relevant file:

 if( '7' === PHP_VERSION[0] ) :  
   echo 'PHP Version 7';
   require_once('Connections/borzoiclubdbPDO.php');
else:
   echo 'PHP Version 5';
   require_once('Connections/borzoiclubdb.php');
endif;

The complete contents of the PHP Version 5 can be copied to the new PDO file version and gradually make changes and test thoroughly.

Once satisfied that everything is OK then upload the new PDO file.

Ah! That makes sense now. Thanks @John_Betong

You and all the others who have been so kindly helping me will be glad to hear that I just did a 25-question “quiz” about php on the w3schools website and only got 2 wrong! I guess it IS possible to teach an old dog new tricks. :smile:

1 Like

I’ve been reading and reading and reading tutorials about using PDO and it’s finally sinking in; I’m almost ready to try converting one page. :slight_smile:

I have a question though: in the snippet above @John_Betong is referencing the Connections file I have already set up. I notice, though, that all the tutorials I’ve been reading say:

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);

… basically inserting the variables right into the page.

It seems to me that referencing an external file (Connections/dbname.php) would be more secure than including all the variables right in the page.

What am I not understanding about PDO?

Using an external file is far better and especially if a single connection file is above the root. This reduces the possibility of users gaining access to the login parameters.

If it is possible on your remote site to store the database connection file above the root then there are numerous ways to include/require the file.

Methods such as relative paths, hard-coding or finding the current path using __DIR__, getcwd() or findings the relevant parameter using:

echo '<pre>'; // adds line feeds to make the data easier to read
print_r( $_SERVER) );
echo '</pre>';

After including/requiring the connection file the $conn variable should be immediately available in the calling file… assuming the connection was successful.

Thanks @John_Betong. I am able to upload above the root (I already have a htpass.pwd file there). :slight_smile: Would I upload just the borzoiclubdb.php file or would I keep it in the Connections folder and upload that folder?

It is preferable to upload the Connections folder and all the contents but there may be problems with other relative paths not being found.

I would be tempted to leave the Connections folder because it is working if only with MySqli/Mysql.

I would also copy the contents of index.php to index-pdo.php and make the necessary changes to completely new folders and files (all with a pdo reference). The idea being to be able to run both Mysql and Pdo with existing data. The old Mysql files and folders can be temporarily renamed and are still available until the new pdo file system has been thoroughly tested and validated.

Edit:
Completed minor editing tweaks :slight_smile:

Actually, the Connections folder does work with PDO. I tried it. :slight_smile:

There are only a few pages on the website which make use of the database, so I’ve just renamed the .php files, adding PDO (NewTitleFormPDO.php, etc). I’ve done the New Title form and its accompanying insert.php file and they work! I originally did them with the connections information ($servername, $username, $password and $dbname) at the top of the page. Then I simply hid those 4 lines and added the <?php require_once('../Connections etc. line, uploaded and it worked!

I did think about doing as you suggest but with the dozens of files which are strictly informational I figured it made more sense just to work on the eleven pages which use the database individually. Actually I’ve done the New Title pages and also the Pedigree Book pages; now I’ll move on to the 9 trophy donation pages. Once I get each “PDO” version to work I simply change the link in the menu file and it goes live.

Thank you (and the others who have weighed in) so much for the help you’ve given.

Oh - and once I get all 11 pages working, then I’ll see if I can get them to work with the Connections folder above the root. For the time being, I’m leaving that folder where it is. I just feel that it’s better to have it separate. If I can get it to work place above the root, even better. :slight_smile:

1 Like