Jump to content

Changing code to PHP 7 compatible help


EWoo

Recommended Posts

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);

?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.