Port php mysql scripts to php 7.0 from 5.x version

2018-04-23 354 words 2 mins read

Recently I got a script or series of scripts that were written for PHP 5.6x and hence used mysql_connect which as you know by now does not work with PHP 7.0. Since there were number of scripts, I thought it would be waste of time to change them manually and wrote a script to fix them. If you have similar situation then probably this few lines could help you.

Since my scripts did not use all the functions so I did not put the sed commands for all of them but you get the idea πŸ™‚

#!/bin/bash - 
#===============================================================================
#
#          FILE: fix_mysql.sh
# 
#         USAGE: ./fix_mysql.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka), 
#  ORGANIZATION: 
#       CREATED: 03/23/2018 16:35
# Last modified: Fri Mar 23, 2018  04:36PM
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error


### Run as 
#### find . -type f -name \*php -exec ~/fixmysql.sh {} \;

####

# mysql_affected_rows -> mysqli_affected_rows($link)
# mysql_close -> mysqli_close($link)
# mysql_data_seek -> mysqli_data_seek( $result, $offset)
# mysql_errno -> mysqli_errno( $link)
# mysql_error -> mysqli_error( $link)
# mysql_fetch_array -> mysqli_fetch_array( $result, $type)
# mysql_fetch_assoc -> mysqli_fetch_assoc( $result)
# mysql_fetch_lengths -> mysqli_fetch_lengths( $result )
# mysql_fetch_object -> mysqli_fetch_object( $result, $class, $params)
# mysql_fetch_row -> mysqli_fetch_row( $result)
# mysql_field_seek -> mysqli_field_seek( $result, $number)
# mysql_free_result -> mysqli_free_result(result)
# mysql_get_client_info -> mysqli_get_client_info( $link)
# mysql_get_host_info -> mysqli_get_host_info( $link)
# mysql_get_proto_info -> mysqli_get_proto_info( $link)
# mysql_get_server_info -> mysqli_get_server_info( $link)
# mysql_info -> mysqli_info( $link)
# mysql_insert_id -> mysqli_insert_id( $link)
# mysql_num_rows ->  mysqli_num_rows( $result)
# mysql_ping -> mysqli_ping( $link)
# mysql_query -> mysqli_query( $link, $query)
# mysql_real_escape_string -> mysqli_real_escape_string( $link)
# mysql_select_db - > mysqli_select_db( $link, $database)
# mysql_set_charset -> mysqli_set_charset( $link, $charset)
# mysql_stat -> mysqli_stat( $link)
# mysql_thread_id -> mysqli_thread_id( $link)
######


file=$1
sed -i 's/mysql_connect/mysqli_connect/g' $file
sed -i 's/mysql_query(/mysqli_query($con,/g' $file
sed -i 's/mysql_error/mysqli_error/g' $file
sed -i 's/mysql_select_db(/mysqli_select_db($con,/g' $file
sed -i 's/mysql_affected_rows/mysqli_affected_rows($con,/' $file
sed -i 's/mysql_escape_string(/mysqli_escape_string($con/' $file 
sed -i 's/mysql_real_escape_string(/mysqli_real_escape_string($con/' $file 
sed -i 's/mysql_fetch_array/mysqli_fetch_array/g' $file
sed -i 's/mysql_fetch_assoc/mysqli_fetch_assoc/g' $file
sed -i 's/mysql_fetch_row/mysqli_fetch_row/g' $file
sed -i 's/mysql_select_db/mysqli_select_db/g' $file


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it