mysql_real_escape_string 适用于本地主机,但不适用于网络服务器
我有一个 PHP 脚本,它在从用户填写的表单中获取数据后将记录插入到数据库中.我在本地机器(WAMP 服务器)上开发,并在我的 PHP 脚本中有以下代码:
I have a PHP script that is inserting a record into a database after getting the data from a user-filled form. I developed on my local machine (WAMP server) and have the following code in my PHP script:
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
//connecting to db
mysql_connect("host", "user", "pass") or die('save_failed');
mysql_select_db("db") or die('save_failed');
//inserting into table
mysql_query("INSERT INTO table(name, email) VALUES('" . $name . "', '" . $email . "' ) ")
or die('save_failed');
脚本按预期工作,记录已成功插入表中.
The script worked just as expected and the records were being successfully inserted into the table.
当我将脚本移动到我的网络服务器时,我意识到存储在数据库中的值是空字符串.删除 mysql_real_escape_string
修复了这个问题.
As soon as I moved the script to my webserver, I realized the values being stored in the database were empty strings. Removing mysql_real_escape_string
fixed this.
为什么 mysql_real_escape_string
在我的网络服务器上不起作用?
Why is it that mysql_real_escape_string
won't work on my webserver?
我正在处理的数据库没有改变.即使在本地开发时,我也遇到了我的网络服务器数据库.本地主机上的 PHP 版本是 5.3.8,而网络服务器上是 5.2.
The database I'm working on hasn't changed. Even when developing locally, I was hitting my webserver DB. PHP ver on localhost is 5.3.8 while on the webserver it is 5.2.
PHP 5.2 是否不支持 mysql_real_escape_string
,如果支持,有什么替代方案?
Does PHP 5.2 not support mysql_real_escape_string
, and if so what is the alternative?
把 mysql_connect("host", "user", "pass") or die('save_failed');
放在 mysql_real_escape_string
.