gpt4 book ai didi

php - 对 mysql_real_escape_string 的每次调用都需要再次访问数据库吗?

转载 作者:可可西里 更新时间:2023-11-01 07:05:38 28 4
gpt4 key购买 nike

http://php.net/manual/en/function.mysql-real-escape-string.php :

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

好吧,基本上如果我做过这样的事情:

mysql_query("insert T(C)select'".mysql_real_escape_string($value)."'")

我要为 mysql_real_escape_string 函数访问数据库 1 次,为 mysql_query 函数访问数据库 2 次?

最佳答案

它使用 mysql 库的事实意味着它与服务器进行往返。

它运行来自 mysql 客户端库的代码,加载在与您的 php 解释器相同的进程中。不过,您确实需要一个连接——该功能需要知道一些服务器设置才能正常运行。但是这些设置缓存在 PHP 端的连接信息中。

如果您想验证这一点(并且您在 linux 上),请编写一个简单的脚本,如:

<?php
$link = mysql_connect('localhost', 'user', 'pass');
echo "Connection done\n";
echo mysql_real_escape_string("this ' is a test");
?>

并通过 strace 运行它:

$ strace php t.php
.... # here comes the connection to mysql, socket fd == 3
connect(3, {sa_family=AF_FILE, path="/var/run/mysqld/mysqld.sock"}, 110) = 0
fcntl(3, F_SETFL, O_RDWR) = 0
setsockopt(3, SOL_SOCKET, SO_RCVTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
.... # talking with mysql here
poll([{fd=3, events=POLLIN}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}])
read(3, "8\0\0\0\n5.1.58-log\0\3\0\0\0K-?4'fL+\0\377\367!"..., 16384) = 60
...
read(3, "\7\0\0\2\0\0\0\2\0\0\0", 16384) = 11
# first php echo
write(1, "Connection done\n", 16Connection done ) = 16
# second php echo
write(1, "this \\' is a test", 17this \' is a test) = 17
munmap(0x7f62e187a000, 528384) = 0
....

唯一重要的是 echo 语句引起的两个 write 之间没有其他系统调用 - 没有系统调用就不可能进行网络通信(来自无论如何在 Linux 中的用户空间)。

关于php - 对 mysql_real_escape_string 的每次调用都需要再次访问数据库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6808952/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com