Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.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 如何在下一项的查询中添加逗号?_Php_Mysql - Fatal编程技术网

Php 如何在下一项的查询中添加逗号?

Php 如何在下一项的查询中添加逗号?,php,mysql,Php,Mysql,如何在下一项的查询中添加逗号?为问题添加更多细节 // Indented the codes to make it more readable $participants = [602, 600]; $query = array(); $query[] = 'INSERT INTO ' . $db->nameQuote( '#__social_conversations_participants' ); $query[] = '(' .

如何在下一项的查询中添加逗号?为问题添加更多细节

// Indented the codes to make it more readable
$participants = [602, 600];

$query      = array();
$query[]    = 'INSERT INTO ' . 
             $db->nameQuote( '#__social_conversations_participants' );
$query[]    = '(' . $db->nameQuote( 'conversation_id' ) .',' . 
             $db->nameQuote( 'user_id' ) . ',' . $db->nameQuote( 'state' ) . ')';
$query[]    = 'VALUES';
foreach( $participants as $userId ){
    $query[]    = '(' . $db->Quote( $conversationId ) . ',' . 
                  $db->Quote( $userId ) . ',' . $db->Quote( 1 ) . ')';
//这不起作用,因为本例中的next()始终返回 假的


从这里找到解决方案:


把精力放在你的问题上。那是什么数据库库?它有参数化的查询吗?为什么要构建这样的查询,为什么要将其拆分为一个数组来重新加入它?@Isaac,itsjoomla@Dagon,做事情的方法有很多我之所以选择这种方法是出于某种原因如果你有更好的方法来解决我的问题,请分享
    // Indented the codes to make it more readable
    if( next( $participants ) !== false ){
        $query[]    = ',';
    }
}
// Glue query back.
$query = implode( ' ' , $query );
var_dump($query);exit;
<?php
    $myString = "Red,Blue,Black";// incoming string comma names
    $myArray = explode(',', $myString); 
    print_r($myArray);
    $sql = "INSERT INTO `cat_interest`(`id`,`categories`) VALUES";
    foreach($myArray as $value){
        $sql .= " (1, '{$value}'),";
    }
    echo rtrim($sql, ',');