Paul-D Posted March 2 Share Posted March 2 Hi. It has been a long time since I have been involved in PHP programming. Getting old. I have received an email from easyspace telling me of urgent upgrafing from 5.4.45 to 7. How much is this going to affect me. My website does not use classes only strait php codeing. Link to comment Share on other sites More sharing options...
Barand Posted March 2 Share Posted March 2 The biggest change will be to your database access. If you use mysql_* functions you will find they no longer exist in version 7. You will need to rewrite all your db code to use either mysqli, or PDO As you re going to have to change and relearn I would recommend you take the PDO path. The name "mysqli" may look like "mysql" but they are different animals and there is more to recoding than just adding "i". Link to comment Share on other sites More sharing options...
ginerjm Posted March 2 Share Posted March 2 Definitely go with PDO. Link to comment Share on other sites More sharing options...
Paul-D Posted March 2 Author Share Posted March 2 Okay thanks for that. Can you send me a link to the new connection strings as I kept mine in a securety section were all pages go to. Link to comment Share on other sites More sharing options...
Barand Posted March 2 Share Posted March 2 try https://phpdelusions.net/pdo_examples Link to comment Share on other sites More sharing options...
benanamen Posted March 2 Share Posted March 2 Php 7 has reached end of life. Tell them to upgrade you to Php 8. 1 Link to comment Share on other sites More sharing options...
mac_gyver Posted March 2 Share Posted March 2 the php documentation lists the major changes going between each version - https://www.php.net/manual/en/appendices.php if there's not a huge amount of code, you could just post the entire project somewhere (github) and someone can review and list what things in it will need to be changed. agree with @benanamen, you should be updating to the latest php version, not php7. Link to comment Share on other sites More sharing options...
Paul-D Posted March 2 Author Share Posted March 2 Okay. Is this what I am going to have to do? <?php error_reporting(E_ALL); ini_set('display_errors', '1'); define ('HOSTNAME1', 'mysql09.iomart.com'); define ('USERNAME1', 'otoogc692'); define ('PASSWORD1', 'mauritius'); define ('DATABASE1', 'otoogc692'); define ('SUGARCAIN' , 1); define ('BLANTYRE' , 2); function connectDB() { $host = HOSTNAME1; $user = USERNAME1; $pass = PASSWORD1; $MyDB = DATABASE1; // OLD //mysql_connect($host, $user, $pass); //mysql_select_db($data, $link); // NEW. Got this a StackOverflow forum $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ ]); } function Contracts($id) { connectDB(); $qList = $pdo->query("SELECT * FROM Contracts"); return $qList; // Calling routine uses while ($row = $qList->fetch()) { // echo $row['name']."<br />\n"; } } // https://phpdelusions.net/pdo_examples ?> Link to comment Share on other sites More sharing options...
Paul-D Posted March 2 Author Share Posted March 2 This should work? but get Notice: Undefined variable: pdo in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/PDOtest2.php on line 29 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); define ('HOSTNAME1', 'mysql09.iomart.com'); define ('USERNAME1', 'otoogc692'); define ('PASSWORD1', 'mauritius'); define ('DATABASE1', 'otoogc692'); define ('SUGARCAIN' , 1); define ('BLANTYRE' , 2); function connectDB() { $host = HOSTNAME1; $user = USERNAME1; $pass = PASSWORD1; $MyDB = DATABASE1; $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ ]); } function Contracts() { connectDB(); $qList = $pdo->query("SELECT * FROM EC_event WHERE Date = '2020-10-20 17:55:00'"); return $qList; // Calling routine uses while ($row = $qList->fetch()) { // echo $row['name']."<br />\n"; } } $Result = Contracts(); $MyRow = $row = $qList->fetch(); echo "Value = " . $MyRow['Venue']; ?> Link to comment Share on other sites More sharing options...
Barand Posted March 2 Share Posted March 2 The variable $pdo has not been defined inside the functioon. You need to pass it to the function when you call it. function Contracts($pdo) { // connectDB(); $qList = $pdo->query("SELECT * FROM EC_event WHERE Date = '2020-10-20 17:55:00'"); return $qList; // Calling routine uses while ($row = $qList->fetch()) { // echo $row['name']."<br />\n"; } } $Result = Contracts($pdo); Don't connect every time you perform a query. Connect once at the top of the script, storing the connection in $pdo. Link to comment Share on other sites More sharing options...
mac_gyver Posted March 2 Share Posted March 2 21 minutes ago, Paul-D said: PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ when you make the connection, you should set the error mode to exceptions, which is what you are doing, set emulated prepared queries to false, you need to add this, and set the default fetch mode to assoc, you are setting it to obj, which is not what you are using in the example code and is probably not what your existing code is using with the fetched data, which will require you to make even more changes to your existing code. Link to comment Share on other sites More sharing options...
Paul-D Posted March 2 Author Share Posted March 2 I can't beleive you said that PHP7 is at end of life. I am using a company called easyspace. They sent me an email that they are upgrading to PHP7 in one weeks time. Link to comment Share on other sites More sharing options...
Barand Posted March 2 Share Posted March 2 See https://www.php.net/supported-versions.php Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 Okay. I have this code which results in Notice: Undefined variable: conn in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/PDOtest2.php on line 25 Fatal error: Call to a member function query() on a non-object in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/PDOtest2.php on line 25 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); define ('HOSTNAME1', 'mysql09.iomart.com'); define ('USERNAME1', 'otoogc692'); define ('PASSWORD1', 'mauritius'); define ('DATABASE1', 'otoogc692'); $host = HOSTNAME1; $user = USERNAME1; $pass = PASSWORD1; $MyDB = DATABASE1; $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ ]); $Result = Contracts(); $MyRow = $row = $Result->fetch(); echo "Value = " . $MyRow['Venue']; function Contracts() { $qList = $conn->query("SELECT * FROM EC_event WHERE Date = '2020-10-20 17:55:00'"); return $qList; } ?> Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 Sorry typo. This should be $Result = Contracts(); $MyRow = $Result->fetch(); echo "Value = " . $MyRow['Venue']; Link to comment Share on other sites More sharing options...
Barand Posted March 3 Share Posted March 3 2 hours ago, Paul-D said: Okay. I have this code which results in Notice: Undefined variable: conn in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/PDOtest2.php on line 25 You've been told how to fix that. If you ignore the information given to you then no there is no point in your posting or our answering Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 Have made alterations from the https://phpdelusions.net/ examples. Still get Undefined variable: conn in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/PDOtest2.php on line 26 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); define ('HOSTNAME1', 'mysql09.iomart.com'); define ('USERNAME1', 'otoogc692'); define ('PASSWORD1', 'mauritius'); define ('DATABASE1', 'otoogc692'); $host = HOSTNAME1; $user = USERNAME1; $pass = PASSWORD1; $MyDB = DATABASE1; $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]); $Result = Contracts(); $MyRow = $Result->fetch(); echo "Value = " . $MyRow['Venue']; function Contracts() { $qList = $conn->query("SELECT * FROM EC_event WHERE ID = 2"); return $qList; } ?> Link to comment Share on other sites More sharing options...
Barand Posted March 3 Share Posted March 3 16 hours ago, Barand said: The variable $pdo has not been defined inside the functioon. You need to pass it to the function when you call it. Except fot the variable name ($pdo -> $conn) this is exactly the same problem that I answered earlier. If you are going to keep repeating the same question then I am going to close this thread. Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 Have made alterations from the https://phpdelusions.net/ examples. Still get Undefined variable: conn in /vhost/vhost15/d/e/s/desmond-otoole.co.uk/www/PDOtest2.php on line 26 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); define ('HOSTNAME1', 'mysql09.iomart.com'); define ('USERNAME1', 'otoogc692'); define ('PASSWORD1', 'mauritius'); define ('DATABASE1', 'otoogc692'); $host = HOSTNAME1; $user = USERNAME1; $pass = PASSWORD1; $MyDB = DATABASE1; $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]); $Result = Contracts(); $MyRow = $Result->fetch(); echo "Value = " . $MyRow['Venue']; function Contracts() { $qList = $conn->query("SELECT * FROM EC_event WHERE ID = 2"); return $qList; } ?> Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 (edited) I did say that I am out of my depth here but if you mean that you close the thread then do so. I am passing it EVERY WHERE NOW. I am asking for help. You could politly tell me this is what you need to do. No chance of that. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); define ('HOSTNAME1', 'mysql09.iomart.com'); define ('USERNAME1', 'otoogc692'); define ('PASSWORD1', 'mauritius'); define ('DATABASE1', 'otoogc692'); $host = HOSTNAME1; $user = USERNAME1; $pass = PASSWORD1; $MyDB = DATABASE1; $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]); $Result = Contracts($conn); $MyRow = $Result->fetch(); echo "Value = " . $MyRow['Venue']; function Contracts($conn) { $qList = $conn->query("SELECT * FROM EC_event WHERE ID = 2"); return $qList; } ?> Edited March 3 by Paul-D Link to comment Share on other sites More sharing options...
Barand Posted March 3 Share Posted March 3 I told you what you need to do Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 (edited) I do not have a $pdo in my code ANYWHERE.. I do have a $conn as in $conn = new PDO ( ... $conn is a connection object is it not. Where is $pdo in my code. The variable $pdo can not be defined in a function if it does not exsist. I am using $conn as an obcect as in $con = NEW Edited March 3 by Paul-D Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 (edited) You are looking at VERY OLD CODE. Look for a $pdo defined as an object in this code. I am doing exactly as you said using $conn. Look at the code I am supplying NOW. I never defined $pdo as an object. Where has $pdo come from? $conn = NEW PDO() makes $conn an object not a $pdo. $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ***** $Result = Contracts($conn); **** function Contracts($conn) { $qList = $conn->query("SELECT * FROM EC_event WHERE ID = 2"); return $qList; } <?php error_reporting(E_ALL); ini_set('display_errors', '1'); define ('HOSTNAME1', 'mysql09.iomart.com'); define ('USERNAME1', 'otoogc692'); define ('PASSWORD1', 'mauritius'); define ('DATABASE1', 'otoogc692'); $host = HOSTNAME1; $user = USERNAME1; $pass = PASSWORD1; $MyDB = DATABASE1; $conn = new PDO("mysql:host=$host; dbname=$MyDB; charset=UTF8", $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]); $Result = Contracts($conn); $MyRow = $Result->fetch(); echo "Value = " . $MyRow['Venue']; function Contracts($conn) { $qList = $conn->query("SELECT * FROM EC_event WHERE ID = 2"); return $qList; } ?> Edited March 3 by Paul-D Link to comment Share on other sites More sharing options...
Paul-D Posted March 3 Author Share Posted March 3 Is mac_gyver on line? Link to comment Share on other sites More sharing options...
Barand Posted March 3 Share Posted March 3 28 minutes ago, Paul-D said: Where has $pdo come from? You tell us. You used it in one of the first sets of code that you posted. You used $pdo and I told you the error was because it wasn't defined in the function and, therefore, you needed to pass it to the function. The problems you are currently having is nothing to do with PHP version or database access. It is a lack of basic knowledge by you on how to use use php functions and variables (variable scope) Link to comment Share on other sites More sharing options...
Recommended Posts