i would like to know on how to convert an array so that the variable can be used to store in database table...
2 1929 pbmods 5,821
Recognized Expert Expert
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 ak1dnar 1,584
Recognized Expert Top Contributor
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,INTE L,DELL);
for($i=0;$i<cou nt($ARRAY);$i++ )
{
$query='insert into products (p_name) values("'.$ARRA Y[$i].'")';
$result=mysql_q uery($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 databases, arrays, etc. but have not worked
with binary data files.
http://www.performancecentral.net/C...3Loop/3Loop.php
click on "Download Performance".
|
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
------- ------- -------- ------- ----- ----- ---
.... ... ... ... ... ... ...
<Add item> <Submit>
|
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 on the "bit = false;" stating that bit is read-only.
|
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 assign that array to a session variable it chokes.
e.g. create array and load each element with a row from the file (btw,
each row is an array as well, using fgetcsv()). When local array is
loaded, I assign to session var as so:
$_SESSION =...
|
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) from a listbox and also set a time period for the search by selecting a start date and end date. Once the query is executed a form letter(s) is (are) generated to the client(s) that details the products that have been purchased during the time...
| |
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;
this produced an error
|
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 serialized array. This works
flawlessly when using only single line text. When I tried to store
multiline text, the problem arose.
When the serialized data is deserialized, the array breaks. Any
suggestions?
|
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}
System.Text.UTF8Encoding str = new System.Text.UTF8Encoding();
string s = new string((char)107, 1);
s += new string((char)62, 1);
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |