Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP SQL查询字符串与MySQLi prepared语句的连接_Php_Sql_Mysqli_Prepared Statement - Fatal编程技术网

PHP SQL查询字符串与MySQLi prepared语句的连接

PHP SQL查询字符串与MySQLi prepared语句的连接,php,sql,mysqli,prepared-statement,Php,Sql,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->

在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

但是,您不应该将其用于此目的。您的数据库结构是对神圣范式的致命罪过

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

userid | friendid
     1 |        2
     1 |        3
     1 |        5
     5 |        1
     5 |        2
     2 |       10
这样,您将需要一个常规的插入查询,如下所示:

INSERT INTO friendslist SET friendid = ?, userid = ? 

在不久的将来,它将为你节省大量的头发。是的,我同意我的设计有很大的缺陷。我想按照你建议的方式来做,但是如果人们选择不同数量的朋友,我怎么知道要创建多少列FriendID呢?我是否需要不断扩展我的表?因此每个用户都需要一个不同的表?然后为每个用户的朋友添加一行?只是添加了表。+1。为了完整性:MySQL中的连接是使用该函数最简单的。