I was used implode function for an array to insert the value into DB.
$day1=implode(',',$day);
echo "day:".$day1;
$day have a value of array like {100,100}; now i change this like 100,100 string to insert into the table. because I can't store array directly into the table.if any option instead of implode() to load the array into DB please post that.
else help me which datatype is used to store in DB . i cann't use int because of commas.
Under the assumption you are using MySQl, you should be utilizing the TEXT data type, because this data type offers no assumptions on what size your insert will be. Now, of course if you want to calculate a fixed length, feel free to use varchar, but because of the unknown width of your numbers, you would have to overallocate regardless.
https://dev.mysql.com/doc/refman/5.5/en/blob.html
Storing it as a string with numbers delimited by a comma is an adequate storage format, and should work fine, as you are dealing with numbers there would be no conflict when retrieving the data and then exploding the value back into an array using the same comma delimiter.
The implode() returns the array items concatenated to a single string (http://php.net/manual/en/function.implode.php).
Your database field needs to be of a text type (char, varchar, text) to store the value.
In this situation you have to convert array into json and store in db.
ex:
$data=("a","b","c");
$json_encode=json_encode($data);
then after retrieved from db convert back to array.
ex:
$json_decode=json_decode($data,true);
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
// Empty string when using an empty array:
var_dump(implode('hello', array())); // string(0) ""
Related
I wanted to store array into my MYSQL columns hence I upgraded my MYSQL version to 5.7. I have created a table with column as JSON type.
My Need is, I will have to store an array in this column.
Hence what I did was,
First.
I want $my_array to be stored in database as json format,
$my_array = ["a","b","c"];
I'm storing it in the Database column lets say alphabets by converting it to JSON using json_encode($my_array).
$words_samples = "hello man!";
$my_array = ["a","b","c"];
$my_array = json_encode($my_array);
I'm using Codeigniter, So I'll write the SQL query that i'm using.
INSERT INTO english ('alphabets', 'words_samples')
VALUES ($my_array, $words_samples);
Insert is successful with database column output as ["a","b","c"]
Now, I need the alphabets and words_sample data retrieved and displayed in a json.
Code :
SELECT * from english;
In my PHP,
echo json_decode($data).
Now the alphabets data is displayed as [\"a\",\"b\",\"c\"] rather than ["a","b","c"]
I see since the alphabets column is already in JSON encoding it to JSON again is giving some other Output.
How do I prevent double parsing of the alphabets data alone?
I am trying to insert values from a multi select drop down in a form to a mysql db column. For example: The drop down would have one or more choices chosen then when the form is posted it would insert the data into one column in a mysql db. I'm stuck at how to insert the data.
If you want to insert in single row then you can use implode() to generate comma separated data, or you can do json_encode() and add to your colum.
Say you get the data as
$data = array("one", "two", "tree");
// output one, two, three
$insert_data = implode(",", $data);
or
$insert_data = json_encode($data);
Thats for inserting data in single column. While retrieving you can do explode() or json_decode() to get the return data and can use them in the multi-select again.
If you want one row for each item then just loop through the array and add them
you can turn the array into a single string with http://us1.php.net/function.implode
$comma_separated = implode(",", $array);
set the type of the column to string, then use the function serialize($array) to convert the array into a string. When you want to get the string back to an array, use unserialize($string)
A couple of things to think about:
If there is a one to many relationship - it shouldn't be in one column, look into multiple tables and changing your database structure.
If you really want to pass in an array you will need to convert it to string using the php built in function implode, then using the built in function explode to retrieve the column from the db
$arr = array('val1','val2');
$string = implode(',',$arr);
//Do db insert
//Do db retrieve
$arr = explode(',',$string);
http://php.net/function.implode
I want to save extra information before sending the total order to Paypal. For each item I have created a single column in my MySQL database where I want to store it. Now I was thinking to save it as an array which I can read later for creating a PHP page. Extra fields are taken from input form fields.
By using an array can I be sure not to mixup information?
You can store the array using serialize/unserialize. With that solution they cannot easily be used from other programming languages, so you may consider using json_encode/json_decode instead (which gives you a widely supported format). Avoid using implode/explode for this since you'll probably end up with bugs or security flaws.
Note that this makes your table non-normalized, which may be a bad idea since you cannot easily query the data. Therefore consider this carefully before going forward. May you need to query the data for statistics or otherwise? Are there other reasons to normalize the data?
Also, don't save the raw $_POST array. Someone can easily make their own web form and post data to your site, thereby sending a really large form which takes up lots of space. Save those fields you want and make sure to validate the data before saving it (so you won't get invalid values).
The way things like that are done is with serializing the array, which means "making a string out of it". To better understand this, have a look on this:
$array = array("my", "litte", "array", 2);
$serialized_array = serialize($array);
$unserialized_array = unserialize($serialized_array);
var_dump($serialized_array); // gives back a string, perfectly for db saving!
var_dump($unserialized_array); // gives back the array again
Use the PHP function serialize() to convert arrays to strings. These strings can easily be stored in MySQL database. Using unserialize() they can be converted to arrays again if needed.
To convert any array (or any object) into a string using PHP, call the serialize():
$array = array( 1, 2, 3 );
$string = serialize( $array );
echo $string;
$string will now hold a string version of the array. The output of the above code is as follows:
a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}
To convert back from the string to the array, use unserialize():
// $array will contain ( 1, 2, 3 )
$array = unserialize( $string );
<?php
$myArray = new array('1', '2');
$seralizedArray = serialize($myArray);
?>
Store it in multi valued column with a comma separator in an RDBMs table.
You can use MySQL JSON datatype to store the array
mysql> CREATE TABLE t1 (jdoc JSON);
Query OK, 0 rows affected (0.20 sec)
mysql> INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');
Query OK, 1 row affected (0.01 sec)
To get the above object in PHP
json_encode(["key1"=> "value1", "key2"=> "value2"]);
More in https://dev.mysql.com/doc/refman/8.0/en/json.html
Hello I have a box with values 2012-21-04. What I would like to do is use 3 select options to display year, month, and day. I would use php to extract each value. How would i approach this?
If you always have the same type of string to work with, just split the strings, using "-" as delimiter:
$arr_date = explode("-",$date); // $date being "2012-21-04"
$arr_date will be an array of three elements containing the three seperated numerical values
Regards,
STEFAN
I have an array of values, and one of the values is another array (see below). I implode the inner-array so that it becomes v0,v1,vx and I'm inserting it into a column of a mysql database where the datatype is SET()
a = array(
"first"=>"foo",
"second"=>array("b","a","r"), // this becomes "second"=>"b,a,r",
"third"=>"bang"
)
My question is which PDO::PARAM_* should I use?
(Initially I would think PARAM_STR, but I'm not sure if PDO will do something that won't work with SET()).
PARAM_STR should do the job correctly. It will escape values and add single quotes around comma separated list.