Uncaught Error: Call to undefined function mysql_escape_string()

119,210

Solution 1

To help you out here... (too long for a comment)

Your require("config.php"); should contain the following:

Sidenote: Use the proper settings for your host.

$link = mysqli_connect("localhost", "username", "mpassword", "database") or die($link);

Then changing your escape functions to use the mysqli_ version of it and passing the connection parameter to it:

$name = mysqli_real_escape_string($link, $_POST['name']);
$lname = mysqli_real_escape_string($link, $_POST['lname']);
$uname = mysqli_real_escape_string($link, $_POST['uname']);
$email1 = mysqli_real_escape_string($link, $email1);
$email2 = mysqli_real_escape_string($link, $email2);
$pass1 = mysqli_real_escape_string($link, $pass1);
$pass2 = mysqli_real_escape_string($link, $pass2);

Again, same thing for the query. Using the i version and passing connection to it as the first parameter.

mysqli_query($link, "INSERT INTO ...

Check for errors on your query using mysqli_error($link);

So you could modify the query to read as

$query = mysqli_query($link, "INSERT INTO ...

and doing

if(!$query){
   echo "Error: " . mysqli_error($link);
   }

Also read the following on Stack in regards to API mixing:

  • Can I mix MySQL APIs in PHP?
  • You can't. mysql_ with mysqli_ or PDO etc. do NOT intermix together. You must use the same one from connecting to querying.

Footnotes.

Passwords

I also noticed that you may be storing passwords in plain text. This is not recommended. If you intend on going LIVE with this at some point, do NOT store passwords as plain text in your database.

Consult the following.

Other links:

Solution 2

Change your PHP version back to PHP 5.6 and it will working fine.

Share:
119,210
Amar Muratović
Author by

Amar Muratović

Blockchain enthusiast and developer; Experimenting with building DApps on Ethereum network; Currently learning Solidity; Trying to solve my own dilemmas related to Blockchain; Enjoying researching and developing new things :))

Updated on July 09, 2022

Comments

  • Amar Muratović
    Amar Muratović almost 2 years

    Fatal error: Uncaught Error: Call to undefined function mysql_escape_string() in C:\xampp\htdocs\phoenixproject\register.php:16 Stack trace: #0 {main} thrown in C:\xampp\htdocs\phoenixproject\register.php on line 16

    How to fix this?

    <?php
    require("config.php");
    ?>
    <?php
    if(isset($_POST['submit'])){
    
    $email1 = $_POST['email1'];
    $email2 = $_POST['email2'];
    $pass1 = $_POST['pass1'];
    $pass2 = $_POST['pass2'];
    
    if($email1 == $email2) {
        if($pass1 == $pass2) {
    //All good. Nastavi broo.
    
    $name = mysql_escape_string($_POST['name']);
    $lname = mysql_escape_string($_POST['lname']);
    $uname = mysql_escape_string($_POST['uname']);
    $email1 = mysql_escape_string($email1);
    $email2 = mysql_escape_string($email2);
    $pass1 = mysql_escape_string($pass1);
    $pass2 = mysql_escape_string($pass2);
    
    mysql_query("INSERT INTO `users` (`id`, `name`, `lname`, `uname`, `email`, `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass1')") or die (mysql_error());
    
    
    
    }else{
      echo "Sorry, your password is not corrext.";
      exit();
    }
    }else{
      echo "Sorry!";
    }
    
    } // brace for submit conditional
    
    $form = <<<EOT
    <form action="register.php" method="POST">
    First Name: <input type="text" name="name" /></br>
    Last Name: <input type="text" name="lname" /></br>
    Username: <input type="text" name="uname" /></br>
    Email: <input type="text" name="email1" /></br>
    Confirm Email: <input type="text" name="email2" /></br>
    Password: <input type="password" name="pass1" /></br>
    Confirm Password: <input type="password" name="pass2" /></br>
    <input type="submit" value="Register" name="submit" />
    </form>
    EOT;
    echo $form;
    
    ?>
    

    Well I know that I was try to mix mysql and mysqli....

    • Funk Forty Niner
      Funk Forty Niner over 8 years
      you posted a similar question stackoverflow.com/questions/34599244/… and was closed with the same stackoverflow.com/questions/12859942/…
    • Funk Forty Niner
      Funk Forty Niner over 8 years
      if you're connecting with mysqli_ (which I suspect you are), you can't mix mysql_ functions. Those different APIs do NOT intermix.
    • Abdulla Nilam
      Abdulla Nilam over 8 years
    • JimL
      JimL over 8 years
      As an alternative to the escape_string function you should use prepared/parameterized queries where you bind parameters to the query instead of escaping them and inserting them directly. php.net/manual/en/mysqli-stmt.bind-param.php
    • Amar Muratović
      Amar Muratović over 8 years
      Fred thanks men... I use a lot of mysql function and I have problems to go on mysqli.... Can you tell me some sorces to make that big change for me :)
    • Funk Forty Niner
      Funk Forty Niner over 8 years
      you're welcome @AmarMuratović start with the manuals php.net/manual/en/book.mysqli.php and php.net/manual/en/book.pdo.php depending on which one you like best, mysqli or PDO. Remember that you can't mix those neither. You must use the same API from connecting to querying. so mysql_ + mysqli_ or PDO do not mix, etc etc. mysqli all the way, or PDO all the way.
    • Funk Forty Niner
      Funk Forty Niner over 8 years
      @AmarMuratović you also should have posted your entire code and we would have been able to pinpoint the real error. Your system may not accept the mysql_ functions anymore, depending on the webserver/PHP version you're running under.
    • Amar Muratović
      Amar Muratović over 8 years
      I use xampp and ATOM code editor... And I really have problem with mysql_function
    • Funk Forty Niner
      Funk Forty Niner over 8 years
      then don't use mysql_ functions, use mysqli_. First establish a connection php.net/manual/en/function.mysqli-connect.php then query php.net/manual/en/mysqli.query.php easy as pie - Plus, I don't know why you are using mysql_, those are old functions and are not very safe to use anymore and will be removed in future PHP releases. So, you would only be wasting your time really and would need to recode everything later on; that isn't a fun thing to do, believe me. ;-)
    • Amar Muratović
      Amar Muratović over 8 years
      Thanks men I'll try... p.s. can I contact you if I had bug :D
    • Funk Forty Niner
      Funk Forty Niner over 8 years
      @AmarMuratović I posted something for you below to help you out.
  • Amar Muratović
    Amar Muratović over 8 years
    $query = "INSERT INTO users (id, name, lname, uname, email, pass) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass1')"; mysqli_query($con, $query); printf ("New Record has id %d.\n", mysqli_insert_id($con));
  • Amar Muratović
    Amar Muratović over 8 years
    I wrote this but I didn't see anything in my base
  • Funk Forty Niner
    Funk Forty Niner over 8 years
    @AmarMuratović my answer contains $link for the connection and unsure if you did change it or not. Plus, add or die(mysqli_error($con)) to mysqli_query() IF $con is the variable for the connection that you are indeed using.
  • Funk Forty Niner
    Funk Forty Niner over 8 years
    @AmarMuratović I noticed your new question. You misinterpreted my comment up here. I also included a different way of checking for errors. if(!$query){ echo "Error: " . mysqli_error($link); } but you didn't do that.
  • Funk Forty Niner
    Funk Forty Niner over 7 years
    why was this downvoted just now? spite votes aren't well-taken here.