PHP SQL查询字符串与MySQLi准备的语句串联


PHP SQL Query string concatenation with MySQLi prepared statement

在PHP-mysqli准备的语句中连接字符串和变量的正确语法是什么?

我现在的代码是:

        if ($test_stmt2=$mysqli->prepare("UPDATE friends SET friendslist=friendslist+','+? WHERE friendsuserid=? ")) { //Insert userid and frienduserid into friends table
               $test_stmt2->bind_param('si', $friendid, $userid); //Bind parameters (friend user id, your own user id)
               $test_stmt2->execute(); //Execute the prepared query
               echo "success";
        }

基本上,我想得到friendslist列中的字符串,在其中添加一个逗号,然后在friends表中的单元格中添加另一个变量字符串($friendid)。

您似乎需要SQL连接。

但是,您不应该将其用于此目的。你的数据库结构是对神圣形态的致命罪行。

请不要在数据库单元格中存储逗号分隔的值。创建一个不同的来存储相应的值。

userid | friendid
     1 |        2
     1 |        3
     1 |        5
     5 |        1
     5 |        2
     2 |       10

这样,您将需要一个像这样的常规插入查询:

INSERT INTO friendslist SET friendid = ?, userid = ? 

它将在不久的将来为您节省大量头发。