i would like to know on how to convert an array so that the variable can be used to store in database table...
2 1918
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: -
$theArray = array(
-
'name' => 'James',
-
'dob' => '1988-04-12',
-
'gender' => 'yes, please'
-
);
-
-
$query = "REPLACE INTO `myTable` (`" . implode('`, `', array_keys($theArray)) . "`) VALUES ('" . implode("', '", $theArray) . "')";
-
mysql_query($query);
-
-
if($err = mysql_error())
-
throw new Exception(array('query' => $query, 'err' => $err));
-
If your array indexes didn't (or couldn't) match your database field names, you'd have to create a map: -
$data = array(
-
'n' => 'John',
-
'b' => '2000-01-01',
-
'c' => 'peacock'
-
);
-
-
$map = array(
-
'name' => 'n',
-
'dob' => 'b',
-
'gender' => 'c'
-
);
-
-
$theArray = array_combine($map, $data);
-
-
// Or, if you don't have PHP 5, or if you can't be sure of the order of the indexes in $data:
-
$theArray = array();
-
foreach($map as $idx => $val)
-
$theArray[$idx] = $data[$val];
-
-
$query = "REPLACE INTO `myTable` (`" . implode('`, `', array_keys($theArray)) . "`) VALUES ('" . implode("', '", $theArray) . "')";
-
mysql_query($query);
-
-
if($err = mysql_error())
-
throw new Exception(array('query' => $query, 'err' => $err));
-
-
http://php.net/array_combine
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]
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
------- ...
|
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...
|
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...
|
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)...
|
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;
...
|
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...
|
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)
|
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}
...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |