本文介绍了PHP/mySQL:如何在mysql查询中连接变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在mysql_query内的PHP中连接文本和变量的正确方法是什么?这是我的尝试:
What is the proper way to concatenate text and a variable in PHP inside a mysql_query? Here is my attempt:
page.'$pageID'
我希望它输出page3
.
这是所有代码(简化后重点放在mysql_query上):
Here is all of the code (simplified to focus on the 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());
}
如果我只是将page.'$pageID'
替换为page3
,则所有代码都可以完美地工作.
All of the code works perfectly if I simply replace page.'$pageID'
with page3
.
推荐答案
您不需要.
. PHP解析双引号("
)字符串,并将变量替换为其值.因此:
You do not need the .
. PHP parses double quoted ("
) strings and replaces the variables with their values. As such:
$pageID = 3;
echo "UPDATE test SET page$pageID = '$pageProgress' WHERE userID = '$userID'";
http://codepad.viper-7.com/uIdqqH
这篇关于PHP/mySQL:如何在mysql查询中连接变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!