2017-07-18 138 views
-4

我有迅速陣列像這樣的保存陣列使用PHP

["002012295915", "00971595502", "8885555512", "5555648583", "4085555270", "0562825196", "01147220964"] 

什麼是保存到MySQL數據庫使用PHP代碼

+2

'json_encode()',然後存儲到像文本數據庫。當你準備再次使用它時,請執行'json_decode()' – GrumpyCrouton

+0

感謝您的快速回放,但是如果您在此區域允許iam很周,您是否有任何示例 –

+0

它實際上是您可以執行的最簡單的事情之一。 'json_encode($ array)'並將其保存到數據庫中,並在稍後使用'json_decode($ arrFromDB,true)' – GrumpyCrouton

回答

0

如果您在保存所有的計劃的最好方式到mysql數組作爲數據庫中的一個字符串,你可以使用mysqli_query,並且mysqli_real_escape_string

保存所有數組作爲一個字符串:

$arr = ["002012295915", "00971595502", "8885555512", "5555648583", "4085555270", "0562825196", "01147220964"]; 
$con = mysqli_connect("localhost","root","pass","db"); 
mysqli_query($con,"insert into TABLE values('".mysqli_real_escape_string($con,$arr)."')"); 

如果您想稍後打印,請使用select from TABLE where

保存本身的每個值(無正則表達式的知識):

//LET'S MAKE THE ARRAY MORE BEAUTIFUL FOR THE LOOP WITHOUT REGEX. 

    $arr = str_replace('[','',$arr); 
    $arr = str_replace(']','',$arr); 
    $arr = str_replace('"','',$arr); 

//NOW THE ARRAY LOOKS LIKE THAT: 002012295915, 00971595502, 8885555512, 5555648583, 4085555270, 0562825196, 01147220964 
//WE JUST NEED TO SPLIT IT NOW 


$new_Array = explode(',',$arr); //DEVIDE ARRAY ELEMENT BY ',' 

//OK NOW WE JUST NEED TO PUT EACH ONE AS A SINGULAR VALUE IN THE DATABASE. 

$con = mysqli_connect("localhost","root","pass","db"); 

foreach($new_Array as $data) 
{ 
    mysqli_query($con,"INSERT INTO TABLE VALUES('$data')"); 

} 
+0

謝謝你對我的這份工作非常感謝 –

+0

不客氣,高興地幫助:) – YAK