0

I've been getting the following error and I don't know how to fix it. Uncaught Error: Call to undefined function mysql_real_escape_string(). I've been reading some articles on here on how to fix it, but I still am having issues doing so. I tried making it mysqli instead of mysql and then I get more errors that I don't know how to fix such as mysqli_real_escape_string() expects exactly 2 parameters. I don't understand that error because I believe I have two parameters. Here is the part of my code where I am having issues.

require_once 'gen.php';
govdocs_cropandresize($dst_img, $src_img);
imagedestroy($src_img);
govdocs_save_image($dst_img, $link, $group);
imagedestroy($dst_img);
unlink($tmp_name);
header('Location: index.php' . ($group != NULL ? '?g=' . $group : ''));

function govdocs_save_image(&$dst_img, $link = '', $group = '1322715600', $quality = 100) {
    $img = imagejpeg($dst_img, $GLOBALS['tmp_name'], $quality);
    $img_str = mysqli_real_escape_string(file_get_contents($GLOBALS['tmp_name'])); 
    $link = mysql_real_escape_string($link);
    $group = mysql_real_escape_string($group);
    $query = "INSERT INTO govdocs_images (`group`, `link`, `image`) VALUES (FROM_UNIXTIME(" . $group . "),'" . $link . "','" . $img_str . "')";
    if (!mysql_query($query)) {
        // The generated image is too big, so reduce the quality a little bit until it is small enough.
        govdocs_save_image($dst_img, $link, $group, $quality - 5);
//      echo mysql_error();
    }
}

Thank you for the help. I am very new to this and have read a lot of articles but still can't figure it out. NOTE: This code did work correctly before upgrading to PHP 7.

  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). ***[These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7.*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 06 '17 at 18:24
  • simple. stop using removed (from php7) library of `mysql_*`. Switch to `mysqli_*` or `PDO` with `prepared statements` – Alive to die - Anant Jan 06 '17 at 18:24
  • 3
    *"before upgrading to PHP 7"* - says it all. – Funk Forty Niner Jan 06 '17 at 18:25
  • ^ they have no choice ^ @Anant – Funk Forty Niner Jan 06 '17 at 18:25
  • @Fred-ii- now i think it's ok :):) – Alive to die - Anant Jan 06 '17 at 18:27
  • PHP7 is a major change from previous version! ___Think before you jump___ Specially if you have been ignoring Deprecation messages for the last 5 years – RiggsFolly Jan 06 '17 at 18:30
  • *"I tried making it mysqli instead of mysql and then I get more errors that I don't know how to fix such as mysqli_real_escape_string() expects exactly 2 parameters."* - Most of the mysqli_ functions require a connection be passed, i.e.: `mysqli_query($con, $query)` and `mysqli_real_escape_string($con, $var)` and `mysqli_error($con)` and was mostly why that failed you. Plus, if you were mixing `mysql_` in with `mysqli_`, well that also failed you; those different APIs do not intermix with each other and the same goes for any other (different) API. You have all your answers now. Enjoy PHP 7! – Funk Forty Niner Jan 06 '17 at 18:36

0 Answers0