[Solved] save array to mysql using php [closed]


If you’re planning on saving all of the array as one string in the database you can just use mysqli_query, and mysqli_real_escape_string:

Saving all of the array as one 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)."')");

And if you want to print it later just use the select from TABLE where.

Saving each value by itself (without regex knowledge):

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

}

2

solved save array to mysql using php [closed]