PHP/mySQL:如何在 mysql 查询中连接变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8232769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP/mySQL: How do a concatenate a variable in a mysql query?
提问by Mark Rummel
What is the proper way to concatenate text and a variable in PHP inside a mysql_query? Here is my attempt:
在 mysql_query 中连接文本和 PHP 中的变量的正确方法是什么?这是我的尝试:
page.'$pageID'
I want it to output page3
.
我希望它输出page3
.
Here is all of the code (simplified to focus on the mysql_query):
这是所有代码(简化为关注 mysql_query):
if ($_POST['pageProgress']) {
$pageProgress = $_POST['pageProgress'];
$pageID = 3;
$userID = 1;
$updateUserProgress = mysql_query("UPDATE test SET page.'$pageID'='$pageProgress' WHERE userID='$userID'") or die(mysql_error());
}
All of the code works perfectly if I simply replace page.'$pageID'
with page3
.
如果我简单地替换page.'$pageID'
为page3
.
回答by Alex Turpin
You do not need the .
. PHP parses double quoted ("
) strings and replaces the variables with their values. As such:
您不需要.
. PHP 解析双引号 ( "
) 字符串并用它们的值替换变量。像这样:
$pageID = 3;
echo "UPDATE test SET page$pageID = '$pageProgress' WHERE userID = '$userID'";
回答by Kato
The problem is that your .'$pageID' is inside the double-quoted string; you don't concatenate this on the MySQL side; it gets parsed long before MySQL ever sees it.
问题是你的 .'$pageID' 在双引号内;你不会在 MySQL 端连接它;它在 MySQL 看到它之前很久就被解析了。
It might be that you were trying to escape the field name for Mysql, in that case, you use backticks.
可能是您试图转义 Mysql 的字段名称,在这种情况下,您使用反引号。
Try:
尝试:
'UPDATE test SET `page'.$pageID.'`=\''.$pageProgress.'\' WHERE...'
Or, much easier on the eyes:
或者,在眼睛上更容易:
"UPDATE test SET `page{$pageID}`='{$pageProgress}' WHERE..."
回答by Andreas Wederbrand
Something like this.
像这样的东西。
mysql_query("UPDATE test SET page" . $pageID . " = '" . $pageProgress . "' WHERE userID = " . $userID)
回答by TelsaBoil
"UPDATE test SET page".$pageID."='".$pageProgress."' WHERE userID='".$userID."';"
"UPDATE test SET page".$pageID."='".$pageProgress."' WHERE userID='".$userID."';"
Dots are in the wrong spot to do it with PHP's string functions.
用 PHP 的字符串函数来做点是错误的。
回答by Pateman
Try
尝试
mysql_query('UPDATE test SET page'.$pageID.'='.$pageProgress.' WHERE userID='.$userID)
回答by Luceos
$updateUserProgress = mysql_query("UPDATE test SET page".$pageID." = '".$pageProgress."' WHERE userID='".$userID."'") or die(mysql_error());
@Marc B ; that's not the question..
@马克乙; 这不是问题。。
回答by AR.
You don't need to concatenate anything. you do need to sanitize your variable from post though.
你不需要连接任何东西。不过,您确实需要从帖子中清理您的变量。
if ($_POST['pageProgress']) {
$pageProgress = mysql_real_escape_string($_POST['pageProgress']);
$pageID = 3;
$userID = 1;
$updateUserProgress = mysql_query("UPDATE test SET page$pageID='$pageProgress' WHERE userID='$userID'") or die(mysql_error());
}