php 未捕获的错误:调用未定义的函数 mysql_escape_string()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34614096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:07:51  来源:igfitidea点击:

Uncaught Error: Call to undefined function mysql_escape_string()

phpmysqlstringfunction

提问by Amar Muratovi?

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

致命错误:未捕获错误:调用 C:\xampp\htdocs\phoenixproject\register.php:16 中未定义的函数 mysql_escape_string() 堆栈跟踪:#0 {main} 抛出在 C:\xampp\htdocs\phoenixproject\register.php在第 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....

好吧,我知道我试图混合使用 mysql 和 mysqli ....

回答by Funk Forty Niner

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

在这里帮助你......(评论太长了)

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

require("config.php");应该包含以下内容:

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:

然后更改转义函数以使用mysqli_它的版本并将连接参数传递给它:

$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 iversion and passing connection to it as the first parameter.

同样,查询也是如此。使用i版本并将连接传递给它作为第一个参数。

mysqli_query($link, "INSERT INTO ...

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

使用检查查询中的错误 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:

另请阅读 Stack 上有关 API 混合的以下内容:

  • 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:

其他链接:

回答by Waqas Ahmad

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

将您的 PHP 版本改回 PHP 5.6,它会正常工作。

回答by Sukhvir Singh

Best is to downgrade the PHP version to 5.6 and all will set. Error will 100% removed then.

最好是将 PHP 版本降级到 5.6,一切都会设置。错误将 100% 消除。

Hope this helps!

希望这可以帮助!

回答by Thanveer

It seems I don`t know which version of PHP you are Using!

好像不知道你用的是哪个版本的PHP!

If Your PHP is 7.x.x then only one thing to do is just rename

如果您的 PHP 是 7.xx,那么唯一要做的就是重命名

$uname = mysqli_real_escape_string($link, $_POST['uname']);

$uname = mysqli_real_escape_string($link, $_POST['uname']);

to

$uname = $_POST['uname'];

$uname = $_POST['uname'];