PdoStatement->bindParam() 上的字符串编码问题?
我正在尝试从 $_REQUEST
var 中获取的字符串执行简单的 SELECT
语句,但我的 PDO 语句似乎不喜欢该字符串格式,为什么?
我的 $_REQUEST
var 包含像 Hello+World
这样的字符串,因此我需要用空格替换 +
来执行我的 SELECT 语句正确。
// the data returned is Hello+World
$phrase = str_replace ("+", " ", $_REQUEST["my_data"]);
$phrase_select = $connection->prepare ("SELECT data_field FROM my_table WHERE phrase = ':phrase'");
$phrase_select->bindParam (":phrase", $phrase, PDO::PARAM_STR);
$phrase_select->execute ();
$data_field = $phrase_select->fetchColumn (); // return nothing
如果我使用字符串“Hello+World
”手动创建SELECT
,它可以正常工作,但如果我使用$_REQUEST[“my_data”]执行此操作
它不起作用,我错在哪里?
如果我打印 $_REQUEST["my_data"]
它会准确返回 Hello+World
I'm trying to perform a simple SELECT
statement from a string taken from a $_REQUEST
var but it seem my PDO statement doesn't like the string format, why?
My $_REQUEST
var contains a string like Hello+World
, so I need to replace +
with whitespaces to do my SELECT
statement correctly.
// the data returned is Hello+World
$phrase = str_replace ("+", " ", $_REQUEST["my_data"]);
$phrase_select = $connection->prepare ("SELECT data_field FROM my_table WHERE phrase = ':phrase'");
$phrase_select->bindParam (":phrase", $phrase, PDO::PARAM_STR);
$phrase_select->execute ();
$data_field = $phrase_select->fetchColumn (); // return nothing
If I make a SELECT
manually with a string "Hello+World
", it works without problems, but if I do it with $_REQUEST["my_data"]
it won't work, where I'm wrong?
If I print $_REQUEST["my_data"]
it return exactly Hello+World
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必在绑定参数周围添加“..”,pdo 会为您完成此操作
you don't have to add the '..' around your bound param, pdo will do that for you