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