Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 手动指定mysql\u real\u escape\u字符串字符集,以便不需要数据库连接-可能吗?_Php_Mysql_Mysql Real Escape String - Fatal编程技术网

Php 手动指定mysql\u real\u escape\u字符串字符集,以便不需要数据库连接-可能吗?

Php 手动指定mysql\u real\u escape\u字符串字符集,以便不需要数据库连接-可能吗?,php,mysql,mysql-real-escape-string,Php,Mysql,Mysql Real Escape String,虽然我理解为什么mysql\u real\u escape\u string需要一个活动的mysql连接来转义各种字符串,但我希望: 以某种方式手动(呃,这里的术语,也许“静态”是正确的术语)将字符集推送到mysql\u real\u escape\u string,这样运行mysql\u real\u escape\u string就不需要活动的db连接 有些人可能会问,为什么我们要在没有活动连接的情况下转义字符串,原因很简单: a) construct query (this is wher

虽然我理解为什么mysql\u real\u escape\u string需要一个活动的mysql连接来转义各种字符串,但我希望:

以某种方式手动(呃,这里的术语,也许“静态”是正确的术语)将字符集推送到mysql\u real\u escape\u string,这样运行mysql\u real\u escape\u string就不需要活动的db连接

有些人可能会问,为什么我们要在没有活动连接的情况下转义字符串,原因很简单:

a) construct query (this is where the mysql_real_escape_string takes place)
b) create hash (md5, md4, sha1, crc32, choose your poison) from said query
c) check cache (memcached) for hash created in step b
d) query cached in memcached -> return cached results -> terminate script
c) query NOT cached in memcached -> connect to mysql -> cache result to memcached + return result
正如您在上面看到的,因为查询的散列是在检查缓存/连接到db之前创建的,如果需要的话。。。mysql\u real\u escape\u字符串在建立数据库连接之前发生

这样做的全部目的是,如果我们能够纯粹从缓存命中执行“整页请求”,那么就不需要连接到mysqld。句号

我们现在有一大组请求,在这些请求中,数据库连接只是为了执行mysql\u real\u escape\u字符串。这是不必要的

因此,总结一下:

我们需要找到一种方法来安全地转义mysql字符串(POST和GET值,因此它需要100%安全),而无需建立db连接


谢谢。

在使用MySQL\u real\u escape\u string()之前需要MySQL连接,否则会生成E级警告错误,并返回FALSE。如果未定义链接标识符,则使用最后一个MySQL连接


如php.net文档网站所示。

我建议采用不同的方法:

function search(array $params) {
    $cacheKey = md5(serialize($params));

    ... search cache ...

}

您不需要在查询上运行哈希,只需在提供的参数上运行即可。每次查询都应该是相同的。

请查看“在使用MySQL\u real\u escape\u string()之前需要一个MySQL连接,否则会生成E\u级别的错误警告,并返回FALSE。如果未定义链接标识符,则使用最后一个MySQL连接。”虽然我们没有使用此答案,但它是正确的(我们可以看到)唯一的方法是这样做。改变需要执行mysql\u real\u escape\u string的代码逻辑,在mysql\u real\u escape\u string之前散列查询,这是唯一的方法,可以避免mysql\u real\u escape\u string一起执行,从而避免数据库连接。谢谢。