473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to convert from array to variable to be stored in database?

i would like to know on how to convert an array so that the variable can be used to store in database table...
May 23 '07 #1
2 1918
pbmods
5,821 Expert 4TB
You've got a bunch of options depending on your application.

If you need to keep the array together, try using serialize to store the array as a string.

If you want to store the values in separate fields, you could name indexes of your array to match the fields in your database table, and then replace them into your database:

Expand|Select|Wrap|Line Numbers
  1. $theArray = array(
  2.     'name' => 'James',
  3.     'dob' => '1988-04-12',
  4.     'gender' => 'yes, please'
  5. );
  6.  
  7. $query = "REPLACE INTO `myTable` (`" . implode('`, `', array_keys($theArray)) . "`) VALUES ('" . implode("', '", $theArray) . "')";
  8. mysql_query($query);
  9.  
  10. if($err = mysql_error())
  11.     throw new Exception(array('query' => $query, 'err' => $err));
  12.  
If your array indexes didn't (or couldn't) match your database field names, you'd have to create a map:

Expand|Select|Wrap|Line Numbers
  1. $data = array(
  2.     'n' => 'John',
  3.     'b' => '2000-01-01',
  4.     'c' => 'peacock'
  5. );
  6.  
  7. $map = array(
  8.     'name' => 'n',
  9.     'dob' => 'b',
  10.     'gender' => 'c'
  11. );
  12.  
  13. $theArray = array_combine($map, $data);
  14.  
  15. //    Or, if you don't have PHP 5, or if you can't be sure of the order of the indexes in $data:
  16. $theArray = array();
  17. foreach($map as $idx => $val)
  18.     $theArray[$idx] = $data[$val];
  19.  
  20. $query = "REPLACE INTO `myTable` (`" . implode('`, `', array_keys($theArray)) . "`) VALUES ('" . implode("', '", $theArray) . "')";
  21. mysql_query($query);
  22.  
  23. if($err = mysql_error())
  24.     throw new Exception(array('query' => $query, 'err' => $err));
  25.  
  26.  
http://php.net/array_combine
May 23 '07 #2
ak1dnar
1,584 Expert 1GB
I really cannot imagine about the way of your Array. how ever you can customize this as per your requirement.

[PHP]<?
$con = mysql_connect('localhost', 'root', 'dba') or die ("Could not connect to the Database");
mysql_select_db('test', $con) or die (mysql_error());

$ARRAY =array(IBM,INTEL,DELL);

for($i=0;$i<count($ARRAY);$i++)
{
$query='insert into products (p_name) values("'.$ARRAY[$i].'")';
$result=mysql_query($query)or die("Query failed : " . mysql_error());
}

if($result){
echo 'DATA INSERTED';
}else{
echo 'INSERTION FAILED';
}

?>[/PHP]
May 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
11
by: Colin Steadman | last post by:
Hope this makes sense! I'm building an ASP page which allows uses to add items to an invoice via a form, ie: Item No Part No Order No Quanity Units Price VAT ------- ...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
4
by: MrBiggles | last post by:
Here's the sitch: I read in a csv file with 60000 lines (20 fields per record), and store the data to a local array. The file is read in and stored just fine and pretty quick. Now, if I try to...
10
by: Jaye | last post by:
Hi. I am a relative newbie to ASP and I am working on an application that uses ASP and an Oracle 9i database. I have a form that allows the user to query the database by selecting a client name(s)...
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
6
by: dawnerd | last post by:
Hello everyone. I have a question, or problem if you will, that I'm sure someone knows the answer to. I have a database that stores information on a given user. The information is stored in a...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.