2016-08-17 5 views
-1

다음 항목을 쿼리 할 때 쉼표를 추가하는 방법은 무엇입니까? 질문에 자세한 내용을 추가하십시오.다음 항목을 쿼리 할 때 쉼표를 추가하는 방법은 무엇입니까?

// 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) . ')'; 

//이 경우에는() 다음은 항상 여기에서 거짓

// Indented the codes to make it more readable 
    if(next($participants) !== false){ 
     $query[] = ','; 
    } 
} 
// Glue query back. 
$query = implode(' ' , $query); 
var_dump($query);exit; 
+0

넣어 노력을 너의 질문. – Darren

+0

어떤 데이터베이스 라이브러리입니까? 매개 변수가있는 쿼리가 있습니까? – Isaac

+0

왜 이런 식으로 쿼리를 작성합니까? 배열에 다시 들어가서 왜 다시 연결해야합니까? –

답변

1

찾을 솔루션을 반환하기 때문에이 작동하지 않습니다 :에 PHP: Insert separated comma string value with as multiple array value Into MySql

<?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, ','); 
관련 문제