1

How to insert all array data into mysql ?

i want to insert all array string in to mysql , can i do that ?

<?PHP
include("connect.php");
for ($i = 0; $i < 10; $i++) 
    {
        $myArray[$i] = $i;
    }
$sql = "INSERT INTO table
( all_array , user )
VALUES
('array($myArray)' , '11');";
$dbQuery = mysql_query($sql);
?>
6
  • 1
    What is a type of all_array? Commented Jun 30, 2014 at 6:10
  • What is your desired result? All values in separate rows, or in the same row somehow or...? Commented Jun 30, 2014 at 6:11
  • You can't store array's in mysql. Commented Jun 30, 2014 at 6:12
  • Joachim Isaksson - all of string array like "1","2","3","4" Commented Jun 30, 2014 at 6:13
  • @user3769723 If you're going to do searches on that array later, you'll get problems if storing an array like that. In that case, you're better off normalizing the database and put each value in a separate row. Commented Jun 30, 2014 at 6:15

4 Answers 4

1

You can save all array in json type. But you should not use mysql_* functions, use PDO.

For example:

<?PHP
include("connect.php");
for ($i = 0; $i < 10; $i++) 
    {
        $myArray[$i] = $i;
    }

$json_array = json_encode($myArray);
$sql = "INSERT INTO table
( all_array , user )
VALUES
('$json_array' , '11');";
$dbQuery = mysql_query($sql);
?>
Sign up to request clarification or add additional context in comments.

11 Comments

are you positive you can store json array in mysql database( i guess you can as text, longtext, binary or blob) but why would you? why not just write it to a json file at that point, its obviously a better alternative. it will be faster fetching from disk, and more importantly doesn't waste db processing power and query cache. storing arrays of anytype in mysql seems like a bad idea to me.
ok, and if i want to convert this data [0,1,2,3,4,5,6,7,8,9] to array again , how can i do ?
@r3wt There are hundred of thousands of times you need to store arrays in database. So the best option is to store them either by json or after serializing. What is bad idea in it?
@user3769723 you can convert to array via json_decode($json_array,TRUE);
@r3wt Yes as you said RDBMS has made our life much easier than writing to hundreds of files. My dear if you are having large array then it will also cost writing a file. You are doing two steps, 1 writing a file doing to one IO operation, 2, storing the name of file in database another IO operation. And same when you need to extract. Why you don't opt to do one step in both cases?
|
1

You can use PHP's implode function to join array elements with a string.

$arrayAsAString = implode(",", $myArray);

NOTE: I just saw @onuri 's answer. So consider this an alternative.

CAVEAT: Performing database queries would be tough, whichever method you choose.

Comments

0

Use php implode function, it will break all the elements of your array into comma separated string. Change your code as below.

<?PHP
include("connect.php");
for ($i = 0; $i < 10; $i++) 
    {
        $myArray[$i] = $i;
    }
$arrayString= implode(", ", $myArray);
$sql = "INSERT INTO table
( all_array , user )
VALUES
('$arrayString' , '11');";
$dbQuery = mysql_query($sql);
?>

Check out this link for details

3 Comments

Shouldn't it be $json_array = implode(", ", $myArray);?
@Thauwa what makes you think implode returns a valid json-string or has anything to do with json?
Umm... I never thought so. Neither did I claim as such. Are you referring to the variable name I used? I was quoting from the answer given.
0

i would do that with json

<?PHP
    include("connect.php");
    for ($i = 0; $i < 10; $i++) 
    {
        $myArray[$i] = $i;
    }

    $myJSON = json_encode($myArray);

    $sql = "INSERT INTO table ( all_array , user ) VALUES ('$myJSON' , '11');";
    $dbQuery = mysql_query($sql);
?>

this will return the array as a JSON-String which can be stored in db. if i select it later i can easy transform the JSON-String back to an array if neede like:

$myArray = json_decode($myJSONString);

1 Comment

ups, i've should read other posts before... someone had the same answere. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.