0

I want to store ids in database like this 1,2,3,4,5 in one field from a while loop. how can i do this.

I am trying this way.

$array = array();

while($row=mysqli_fetch_assoc($result)){
$array[] = array($row['id']);
}

$query = mysqli_query($connection,"INSERT INTO temp (temp) VALUES('$array')");
print_r($array);

This is showing error Array to string conversion and value as Array is inserted in database.

Please see and suggest any possible way to do this.

Thanks.

3
  • Why not use json_encode ? :) Commented Jan 17, 2013 at 11:47
  • I recommend using serialize($array) to convert into string, and then unserialize($string) to transform it back into an array. Commented Jan 17, 2013 at 11:48
  • 2
    I recommend normalising the database - SO is filled with questions asking how to deal with the problems created by storing arrays like this Commented Jan 17, 2013 at 11:51

8 Answers 8

4

Create another table and insert 5 rows into it.

entity_id | ids
1             1
1             2
1             3
1             4
1             5

that's the only proper way of doing such things

However, if it's really a temp table, there is probably a way to avoid these inserts at all.

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't it a bit too much to use another table just to store an array of plain values?
This is the correct way +1. Everything else shows that someone dont understand how to use sql. en.wikipedia.org/wiki/First_normal_form
i completely agree that this is the correct way, but has OP not mentioned in his question itselt what he wants? Anyways...
3

You can use either serialize or json_encode:

$data = serialize($array);
// ...or...
$data = json_encode($array);

If the array just contains plain numbers you can also use implode:

$data = implode(',', $array);

2 Comments

There are only realy less reasons why to do this. Sometimes you have big data blocks. The you should use serialize or json_encode. If implode is possible ever use a socond table!
Then you should use one of the first two methods.
1
$arrays = array();

while($row=mysqli_fetch_assoc($result)){
    $arrays[] = $row['id'];
}
$arr_str  = implode(",", $arrays);
$query = mysqli_query($connection,"INSERT INTO temp (temp) VALUES('$arr_str')");
//print_r($array);

Detailed information about implode can be found here.

5 Comments

@MihaiIorga: with all due respect, have you read the question? Its clearly mentioned there that "I want to store ids in database like this 1,2,3,4,5 in one...". I know, that what i have suggested is not good code in all scenarios but here he has clearly stated what is needed.
This answer is the third duplicate. -1
@GreenRover: when i wrote this answer, there was none on the page. When i posted and page loaded, i saw there were other answers too. You can find out by time posted. And, i am not repo-hungry. You can down-vote or do anything you like. Please dont mind.
@TallboY: answer that i have specified here is for the scenario OP specified in condition. This may not work in all scenarios. You have array within array, so you might have to apply different logic.
got it working like $array[] = $row['id'];, insted if $array[] = array($row['id']);
0

Use the implode() function:

$to_insert = implode(',', $array);
$query = mysqli_query($connection,"INSERT INTO temp (temp) VALUES('$to_insert')");

2 Comments

Did you read the question? He clearly indicates the contents of his array (i.e. 1,2,3,4,5), so this solution is still valid. Of course, if there were strings, an alternate solution would be required...
At least you're big enough to accept that, unlike most people on the Internet these days... :-P
0
$query = mysqli_query($connection,"INSERT INTO temp (temp) VALUES(".join(',', $array).")");

Comments

0

Try:

$ids = join(',',$array);
$query = mysqli_query($connection,"INSERT INTO temp (temp) VALUES('$ids')");

Comments

0

Please use implode function before insert into database :-

Here is a example :-

 $array = array(1,2,3);
 $comma_separated = implode(",", $array);

 echo $comma_separated; 

Output :-

  1,2,3

In your case :-

    $array = array();

    while($row=mysqli_fetch_assoc($result)){
    $array[] = array($row['id']);
    }
    $comma_separated = implode(",", $array);
    $query = mysqli_query($connection,"INSERT INTO temp (temp) VALUES('$comma_separated')");
    print_r($array);

2 Comments

If comma separated value in your array then serialize should be better
I have sent above example because you have mentioned in your question you need output like 1,2,3,4,5
0

Set the field type varchar(255) and do this with php

$array = array();
$value = serialize($array);

$query = mysqli_query($connection,"INSERT INTO temp (temp) VALUES('$value')");
print_r($array);

To retrieve the value you should unserialize the column

$data = unserialize($result['temp']);

Here result is associative array. $data is an array you dont have to implode or explode or json_encode or json_decode

Comments

Your Answer

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