简体   繁体   English

mysql_real_escape_string问题?

[英]mysql_real_escape_string problem?

This is the first time this has ever happened, and its really weird. 这是有史以来第一次,而且真的很奇怪。

We have been using this function for years, and we are using it everywhere on this site.. although one particilar part of the site - it is causing mySQL errors. 我们已经使用此功能多年,并且在此站点的任何地方都使用此函数。虽然该站点的一个特定部分-它导致了mySQL错误。

function noescape($str) {
$noescape = mysql_real_escape_string($str);
return $noescape;
}

$email = noescape($_POST['email']);

mysql_query("INSERT INTO `contact` (`email`) VALUES('" . $email . "') ", $database) or die(mysql_error());

Yes, $database is defined and it is the right connection info - it is used in other areas of the site. 是的,已经定义了$database ,它是正确的连接信息-在站点的其他区域中使用。

Error: 错误:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/user/public_html/config.php on line 27 警告:mysql_real_escape_string()[function.mysql-real-escape-string]:在第27行的/home/user/public_html/config.php中无法建立到服务器的链接

Infact, the error happens on this line: 实际上,此行发生错误:

$email = noescape($_POST['email']);

It is nothing to do with the query. 与查询无关。

I found this: http://www.resourcebookingpro.com/index.php?option=com_kunena&Itemid=66&func=view&catid=3&id=841#844 我发现了这一点: http : //www.resourcebookingpro.com/index.php?option=com_kunena&Itemid=66&func=view&catid=3&id=841#844

What is wrong?? 怎么了??

Things above this in the page are using the same connection and it is working fine... 该页面上方的内容正在使用相同的连接,并且工作正常……

Well, you have to check the code above this line to see where connection gets closed. 好吧,您必须检查此行上方的代码以查看连接在何处关闭。

I think you must specified mysql link correctly (as said in comments) 我认为您必须正确指定mysql链接(如评论中所述)

function noescape($str) {
global  $database;
$noescape = mysql_real_escape_string($str, $database);
return $noescape;
}

$email = noescape($_POST['email']);

mysql_query("INSERT INTO `contact` (`email`) VALUES('" . $email . "') ", $database) or die(mysql_error());

The real escape string method does rely on db connection. 真正的转义字符串方法确实依赖于数据库连接。 I believe $database is global to the application, so simply put a if statement in noescape method to test if its null like so: 我相信$ database对于应用程序是全局的,因此只需将if语句放在noescape方法中以测试其null是否是这样的:

if($database)
{
  $noescape = mysql_real_escape_string($str, $database);
  return $noescape;
}

I agree with everyone else's thoughts; 我同意其他人的想法; but I do think you should check for null. 但我确实认为您应该检查null。

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM