2014-01-05 27 views
1

我見過很多使用foreach和implode函數循環遍歷數組的示例,爲MySQL插入查詢構建sql字符串。但是,所有這些示例都假定數組中的所有值和MySQL表中的所有列都是字符串或數字。有沒有辦法做類似的事情,但是有一個既有字符串又有數字的數組,並且要插入到具有字符串和數字列的MySQL表中?將具有字符串和數字的多維數組插入到MySQL

$insertArr[] = array(
     'order_id' => $orderID, 
     'user_id' => $userID,   
     'price' => $price, 
     'quantity' => $quantity); 

$colNames = "(" . implode(", ",array_keys($insertArr[0])) . ")"; 
foreach ($insertArr as $row) 
{   
    $colValuesArr[] = "('" . implode("', '", $row) . "')"; 
} 
$colValues = implode(", ", $colValuesArr); 
$strsql = "INSERT INTO tbl_orders $colNames 
      VALUES $colValues"; 

上面的代碼是不工作的權利,因爲order_iduser_id是字符串,pricequantity是數字。使用引號和逗號對它進行內插假設所有值均爲字符串,但如果不帶引號則假設所有值都是數字。有沒有辦法,而不必遍歷數組中的每一個元素,來做到這一點?

+0

變量'$ strsql'具有無效的SQL語法。這可以解釋爲什麼代碼無法正常工作。 –

+1

MySQL引用的數字很好。如果你想保持簡單,只需引用一切。 (但請記住要逃避您的數據!)或者,查看準備好的語句;從長遠來看,他們會讓事情變得更容易。 – cHao

+0

Gordon Linoff,$ strsql SQL語法有什麼問題?這對我來說似乎很好。我印了它,沒有看到有什麼不妥。你可以說得更詳細點嗎? – user2395238

回答

1

假設這是你想要處理的東西,&你不想循環數組或使用預處理語句,我認爲這應該工作。我重新格式化了您的代碼,但總體邏輯仍然完好無損。我添加的邏輯是基本上創建兩個單獨的數組。一個用於數值。其他文本值:

// Test data. 
$orderID = 'testorderid'; 
$userID = 'testuserid'; 
$price = 7.99; 
$quantity = 2; 

// Create the text values array. 
$insertArrText[] = array('order_id' => $orderID, 'user_id' => $userID); 

// Create the numerical values array. 
$insertArrNumerical[] = array('price' => $price, 'quantity' => $quantity); 

// Get the array keys & merge them into one combined array. 
$array_keys = array_merge(array_keys($insertArrText[0]), array_keys($insertArrNumerical[0])); 

// Set the column names. 
$colNames = "(" . implode(", ", $array_keys) . ")"; 

// Loop through the '$insertArrText' 
foreach ($insertArrText as $key => $row) { 

    // Set the numerical values. 
    $numerical_values = implode(", ", $insertArrNumerical[$key]); 

    // Set the text values. 
    $text_values = "'" . implode("', '", $row) . "'"; 

    // Set the column values array. 
    $colValuesArr[] = "(" . $text_values . "," . $numerical_values . ")"; 
} 

// Set the column values string. 
$colValues = implode(", ", $colValuesArr); 

// Create the MySQL query. 
$strsql = "INSERT" 
     . " INTO tbl_orders " . $colNames 
     . " VALUES " . $colValues 
     ; 

// Echo the output for testing. 
echo $strsql; 

該腳本的輸出是:

INSERT INTO tbl_orders (order_id, user_id, price, quantity) VALUES ('testorderid', 'testuserid',7.99, 2) 

編輯下面是一個使用gettype檢測字符串類型返工。更靈活&健壯。這當然是使用測試數據,但應該很容易適應真實世界的情況。

// Test data. 
$orderID = 'testorderid'; 
$userID = 'testuserid'; 
$price = 7.99; 
$quantity = 2; 

// Create an array map based on strings & MySQL DB field values. 
$array_map = array(); 
$array_map['orderID'] = 'order_id'; 
$array_map['userID'] = 'user_id'; 
$array_map['price'] = 'price'; 
$array_map['quantity'] = 'quantity'; 

// Create the text arrays. 
$insertArrText = array(); 
$insertArrNumerical = array(); 

// Set arrays for text and numberical types. 
$text_types = array('string'); 
$numerical_types = array('double','integer'); 

// Lopop through the array map & assign values based on type. 
foreach ($array_map as $array_map_key => $array_map_value) { 
    if (in_array(gettype($$array_map_key), $text_types)) { 
    $insertArrText[0][$array_map_value] = $$array_map_key; 
    } 
    else if (in_array(gettype($$array_map_key), $numerical_types)) { 
    $insertArrNumerical[0][$array_map_value] = $$array_map_key; 
    } 
} 

// Get the array keys & merge them into one combined array. 
$array_keys = array_merge(array_keys($insertArrText[0]), array_keys($insertArrNumerical[0])); 

// Set the column names. 
$colNames = "(" . implode(", ", $array_keys) . ")"; 

// Loop through the '$insertArrText' 
foreach ($insertArrText as $key => $row) { 

    // Set the numerical values. 
    $numerical_values = implode(", ", $insertArrNumerical[$key]); 

    // Set the text values. 
    $text_values = "'" . implode("', '", $row) . "'"; 

    // Set the column values array. 
    $colValuesArr[] = "(" . $text_values . "," . $numerical_values . ")"; 
} 

// Set the column values string. 
$colValues = implode(", ", $colValuesArr); 

// Create the MySQL query. 
$strsql = "INSERT" 
     . " INTO tbl_orders " . $colNames 
     . " VALUES " . $colValues 
     ; 

// Echo the output for testing. 
echo $strsql; 

而且這段代碼的輸出是:

INSERT INTO tbl_orders (order_id, user_id, price, quantity) VALUES ('testorderid', 'testuserid',7.99, 2) 
+0

這可能會工作,如果它只是爲了一個特定的目的。但是,我正在嘗試構建一個我可以隨時使用的函數,並且每次數組可能不同時,都會使用不同的值類型。 – user2395238

+0

那麼,如何使用'gettype'來確定一個值是一個字符串還是一個整數,然後根據該值將它放置在文本或數值數組中? – JakeGould

+0

你會如何使用準備好的語句? – user2395238

0

使用array_map:

$row = array_map(function ($a) { 
    if (is_numeric($a)) return $a; 
    else return "'".$a."'"; 
},$row); 

假設你的數據是安全的,否則請使用類似mysql_real_escape或使用PDO預處理語句。