Page 1 of 1

Update to PHP 7.1 some scripts not working

Posted: Fri 23 Feb 2018 10:24 pm
by PaulMy
I have previously read some of the posts on PHP 7.x updates but not paid a lot of attention.
I had been using PHP v.5.6 and today decided to try v.7.1 and some scripts are no longer working.
http://www.komokaweather.ca/wxhistorical-colour1.php
http://www.komokaweather.ca/betel_datasummary.php
http://www.komokaweather.com/mysql/monthly-temp.php
http://www.komokaweather.com/mysql/monthly-data.php

There may be more not working but these are the first I found.
What do I look for to see what code is at fault, and how to correct?

Thanks for any help and guidance.
Paul

Re: Update to PHP 7.1 some scripts not working

Posted: Fri 23 Feb 2018 10:54 pm
by ConligWX
Firstly check your apache error logs, they should give a definition of the error.

Re: Update to PHP 7.1 some scripts not working

Posted: Fri 23 Feb 2018 11:00 pm
by saratogaWX
From the PHP.net website
PHP 5 users can choose between the deprecated mysql extension, mysqli, or PDO_MySQL. PHP 7 removes the mysql extension, leaving only the latter two options.
If your programs are running mysql calls, they'll have to be converted to mysqli calls to run on PHP7+

You might tryhttps://www.phpclasses.org/blog/package/9199/po ... ml#convert for some help in doing the conversions mysql->mysqli

Re: Update to PHP 7.1 some scripts not working

Posted: Sat 24 Feb 2018 1:33 am
by PaulMy
Thanks Simon and Ken,
This may be larger than I can handle but will start and make an effort... and hopefully learn from it.

The monthly_temp.php and monthly-data.php scripts both start with the same lines 1 through 37 which includes

Code: Select all

<?php 

		//include the information needed for the connection to MySQL data base server. This file contains the databasename,hostname, usernme, and password. For an alternative method:http://php.about.com/od/phpwithmysql/qt/connect_sql_php.htm or google connect to mysql using php 
		include ("dbconfig.php");
		// require_once("cumuluswebtags.php");


		// connect to the MySQL database server 
		$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); 
		 
		 
		// select the database 
		mysql_select_db($database) or die("Error connecting to db."); 
I have looked at the tutorial link Ken provided and presume if I make these changes
Script line 32: // connect to the MySQL database server
Existing line 33: $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
Modify line 33: $db = mysqli_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysqli_connect_error());
Tutorial example old: $connection = mysql_connect( 'host', 'username', 'password', new_link,flags);
Tutorial example new: $connection = mysqli_connect( 'host', 'username', 'password', 'database', 'port', 'socket');

Script line 36: // select the database
Existing line 37: mysql_select_db($database) or die("Error connecting to db.");
Modify line 37: mysqli_select_db($database) or die("Error connecting to db.");
Tutorial example old: $database = mysql_select_db( 'database', $link);
Tutorial example new: $database = mysqli_select_db($link, 'database');
Further the scripts have somewhat similar code for getting the data

Code: Select all

	// the actual query for the grid data 
		$SQL = "SELECT LogDate,  Year(LogDate) as Year, maxtemp, max(IF(month(logdate)=1, maxtemp,null)) as January, max(IF(month(logdate)=2, maxtemp,null))as February, max(IF(month(logdate)=3, maxtemp,null))as March, max(IF(month(logdate)=4, maxtemp,null))as April, max(IF(month(logdate)=5, maxtemp,null))as May, max(IF(month(logdate)=6, maxtemp,null))as June, max(IF(month(logdate)=7, maxtemp,null))as July, max(IF(month(logdate)=8, maxtemp,null))as August ,max(IF(month(logdate)=9, maxtemp,null))as September,max(IF(month(logdate)=10, maxtemp,null))as October, max(IF(month(logdate)=11, maxtemp,null))as November, max(IF(month(logdate)=12, maxtemp,null))as December, max(maxtemp) as total FROM Dayfile  group by year";
		$resultmaxtemp = mysql_query($SQL)or die('Error: ' . mysql_error()); 

		$SQL = "SELECT LogDate,  Year(LogDate) as Year, avgtemp, round(avg(IF(month(logdate)=1, avgtemp,null)),1) as January, round(avg(IF(month(logdate)=2, avgtemp,null)),1)as February, round(avg(IF(month(logdate)=3, avgtemp,null)),1) as March, round(avg(IF(month(logdate)=4, avgtemp,null)),1)as April, round(avg(IF(month(logdate)=5, avgtemp,null)),1)as May, round(avg(IF(month(logdate)=6, avgtemp,null)),1)as June, round(avg(IF(month(logdate)=7, avgtemp,null)),1)as July, round(avg(IF(month(logdate)=8, avgtemp,null)),1)as August, round(avg(IF(month(logdate)=9, avgtemp,null)),1) as September, round(avg(IF(month(logdate)=10, avgtemp,null)),1) as October, round(avg(IF(month(logdate)=11, avgtemp,null)),1) as November, round(avg(IF(month(logdate)=12, avgtemp,null)),1)as December, round(avg(avgtemp),1) as total FROM Dayfile group by year";
		$resultavgtemp = mysql_query($SQL)or die('Error: ' . mysql_error()); 

etc....
and presume I can replace all the occurrences of mysql_select, mysql_query, mysql_fetch and mysql_error with mysqli_select, mysqli_query, mysqli_fetch and mysqli_connect_error.

Before I start to make those changes does that appear to be correct?

Thanks for your patience and guidance,
Paul

Re: Update to PHP 7.1 some scripts not working

Posted: Sat 24 Feb 2018 4:47 am
by saratogaWX
Alas, I am not an expert in using SQL (nor do I have it currently running on my weather website), so far be it from me to counsel you on the conversion.
Others, with more experience, will need to give that advice...

Re: Update to PHP 7.1 some scripts not working

Posted: Sat 24 Feb 2018 6:46 am
by sfws
PaulMy wrote:Further the scripts have somewhat similar code for getting the data
Paul - the right hand side of various "$SQL=" retrievals are indeed pure SQL instructions and therefore unaffected by you changing PHP version.
PaulMy wrote:presume I can replace all the occurrences of mysql_select, mysql_query, mysql_fetch and mysql_error with mysqli_select, mysqli_query, mysqli_fetch and mysqli_connect_error.
I don't know the specific scripts that you are using, so the following comments are based on my limited PHP experience with updating other scripts.

To the best of my knowledge, the parameters for 'mysqli' are mostly the same as for the obsolete 'mysql' , so indeed a global replacement that swaps the former for the latter should work in the majority of cases. However, as you have already noticed, the parameters are in a different order for selecting the database! However, I would use an alternative syntax if it was my code, replacing

Code: Select all

mysql_query
with

Code: Select all

$mysqli->query
My alternative for your lines 33 to 37 in one instruction is

Code: Select all

$db = new mysqli($dbhost, $dbuser, $dbpassword, $database) or die("Connection Error: " . mysqli_error());
The error message that this outputs will say whether the error is in the connection parameters or in finding the database, so you don't need two separate stages as per original.

Notepad++ (that I have just been using for this functionality in a non database context) has "replace in all open files" (start in its search menu), and similar functionality is available in some other editors.

[Post edit - I accidentally typed 'mysql' instead of 'mysqli' in my code example - sorry about that]

Re: Update to PHP 7.1 some scripts not working

Posted: Sat 24 Feb 2018 6:55 am
by BeaumarisWX
Hi Paul,
Sent you PM, hope it helps. Sorry no time for more help :cry: .
Kind regards,