EWoo Posted September 25, 2019 Share Posted September 25, 2019 Hey all, I am not familiar with php coding at all and was wondering if anyone could help me re-code the below to being php 7 compatible. After using a php testor, it says mysql_connect is deprecated and needs to use the alternative mysqli_connect instead. How would I rewrite the below to be in the mysqli_connect format? Thank you in advance <?php $db = "MyDatabaseName"; //set this to the name of your database $host = "localhost"; //set this to the name of your host $user = "MyUserName"; //set this to your db user name $pass = "MyPassword"; //set this to your db password $con = mysql_connect("$host","$user","$pass"); $table = "MyDatabaseTableName"; //set this to the name of the table that holds the card info. $con= mysql_connect("$host","$user","$pass"); if(!$con) { $connect_error = "Failed at mysql_connect. "; echo"error at db connect"; exit(); } $tablewidth = "490";//set the main table width on the spread pages with this. $imagedirectory = "images/"; //main image directory $largeimagedirectory = "images/medium/"; srand((double)microtime()*1000000); ?> Quote Link to comment https://forums.phpfreaks.com/topic/309278-changing-code-to-php-7-compatible-help/ Share on other sites More sharing options...
Barand Posted September 25, 2019 Share Posted September 25, 2019 see https://www.php.net/mysqli_connect Quote Link to comment https://forums.phpfreaks.com/topic/309278-changing-code-to-php-7-compatible-help/#findComment-1569961 Share on other sites More sharing options...
gw1500se Posted September 25, 2019 Share Posted September 25, 2019 Correct. The mysql extensions have been deprecated for decades. I suggest switching to PDO although mysqlI is also available but IMO as a second choice. Quote Link to comment https://forums.phpfreaks.com/topic/309278-changing-code-to-php-7-compatible-help/#findComment-1569962 Share on other sites More sharing options...
EWoo Posted September 26, 2019 Author Share Posted September 26, 2019 Honestly, it is a completely different language to me. I tried changing a few things to mysqli but wasn't able to convert it correctly- at least I think. Is there anyone on here that I can hire to convert this, along with another deprecated code? Thank you much Quote Link to comment https://forums.phpfreaks.com/topic/309278-changing-code-to-php-7-compatible-help/#findComment-1569976 Share on other sites More sharing options...
mac_gyver Posted September 26, 2019 Share Posted September 26, 2019 it takes more than just converting the database statements to update old code. due to the removal of magic_quotes, that provided some security in external string data, you must add protection against sql special characters in data from breaking the sql query syntax, which is how sql injection is accomplished. if you have a large amount of code that needs to be updated, creating user written functions that use the PDO extension internally to replace the mysql_ functions, via search/replace, then change any query with external data being put directly into it, into a prepared query, will result in the least amount of overall changes to the code. if you only have a small amount of code that needs to be updated, just directly re-writing it to use the PDO extension is the best solution. while you are making these changes, switch to use exceptions of errors and in most cases let php catch and handle the exception, where it will use its error related settings to control what happens with the actual error information (database errors will get displayed or logged the same as php errors.) this will let you eliminate, rather than convert, the existing error handling, which is only giving real visitors information they cannot do anything about, and giving hackers useful feedback that something they did caused a specific type of error. Quote Link to comment https://forums.phpfreaks.com/topic/309278-changing-code-to-php-7-compatible-help/#findComment-1569977 Share on other sites More sharing options...
cyberRobot Posted September 26, 2019 Share Posted September 26, 2019 8 hours ago, EWoo said: I tried changing a few things to mysqli but wasn't able to convert it correctly- at least I think. If you post the updated code and the error message(s), we should be able to let you know why it isn't working. 8 hours ago, EWoo said: Is there anyone on here that I can hire to convert this, along with another deprecated code? If this is the route you want to go, you could try the Job Offerings forum:https://forums.phpfreaks.com/forum/77-job-offerings/ Note the Rules and Guidelines post near the top of the forum. Quote Link to comment https://forums.phpfreaks.com/topic/309278-changing-code-to-php-7-compatible-help/#findComment-1569986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.