PHP 5 to 7 upgrade, MySQL connection - help my host is to update PHP |
PHP 5 to 7 upgrade, MySQL connection - help my host is to update PHP |
jacob9500 |
Jun 21 2016, 04:26 PM
Post
#1
|
Group: Members Posts: 9 Joined: 20-July 07 Member No.: 3,399 |
Hello.
My webhost is about to change from using PHP 5, into version 7. I know it will bring trouble in my code, where I make MySQL connections. The host changed PHP for another domain of mine earlier, and I had to write some line to the .htaccess -file. That fixed it, but will only work for a short periode. So I have to change all the connections I make to the database. - And that is a lot! I am not sure what I have to fix, besides that it is about the database, in my case. I also believe that it will be best if I updated to version 7 on my own machine, where I make the scripts. So I ask, in hope you can help me – telling me how to update to v.7. of PHP, and also how to make the actual code, the connection to MySQL, as it is to be done in v.7. I have installed a LAMP stack from Bitnami.com. Now my PHP is. 5.6.19. Mysql is v. 5.0.11 – I think.. Mysqli is the same. From echo phpinfo(); Thanks. Jacob A typical connection looks like so; $mysqli = new mysqli('127.0.0.1','username','password','databasename'); if ($mysqli->connect_error) { die('Error'); } $results = $mysqli->query("SELECT * FROM users WHERE nick='" . $name . "'"); while($row = $results->fetch_assoc()) { $dbnick = $row['nick']; $dbpassw = $row['pass']; ... } Or like this; $con = mysqli_connect('127.0.0.1', 'username', 'password', 'databasename); if (mysqli_connect_errno()) { die('Error'); } $SQL_Streng = "INSERT INTO tablename (title, dateannounced, startdate, closingdate) VALUES ('$title', '$dato', '$startdate', '$closingdate')"; mysqli_query($con, $SQL_Streng); mysqli_close ($con); |
CharlesEF |
Jun 21 2016, 05:02 PM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I don't have PHP7 installed yet but it is my understanding that the only database code that had to be rewritten involved mysql_* functions.
|
jacob9500 |
Jun 21 2016, 07:04 PM
Post
#3
|
Group: Members Posts: 9 Joined: 20-July 07 Member No.: 3,399 |
Ahh. Okay. I just picked a sample of the code I use in the next domain. If that works with PHP7 that is great. At the other domain I used like following; $con = mysql_connect('127.0.0.1', "username", 'password'); mysql_select_db('databasename', $con); ..and.. $result=mysql_query("SELECT count(*) AS total FROM tablename ORDER BY AutoID DESC ", $con); $row = mysql_fetch_array($result); And I found out, the hard way, that it didnt work. Suddenly my site didnt work. I didnt know what the differense was. Because of your reply, and thanks for that, I looked at the code for my other site. And it seems that I use different code, like you see in the small samles I have put here. Am I right then if I simply change to the use of mysqli_* ? I mean will that work in PHP7 ? Thanks a lot. Jacob |
CharlesEF |
Jun 21 2016, 07:13 PM
Post
#4
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Yes, your other site used mysql_* functions so that is why it didn't work with PHP7. Your site that uses mysqli_* functions should have no problem. Any new code you write should use mysqli with prepared statements OR PDO, to help protect against SQL injection attacks.
|
Christian J |
Jun 21 2016, 07:57 PM
Post
#5
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
I see nothing about MySQL here, but some other changes:
http://php.net/manual/en/migration70.incompatible.php |
CharlesEF |
Jun 21 2016, 08:25 PM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
mysql_* functions have been deprecated for years. PHP7 has finally removed them completely.
|
jacob9500 |
Jun 22 2016, 01:51 PM
Post
#7
|
Group: Members Posts: 9 Joined: 20-July 07 Member No.: 3,399 |
Thank you all.
I will change the code ASAP. .. Jacob |
Lo-Fi Version | Time is now: 25th April 2024 - 12:38 PM |