473,386 Members | 1,738 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,386 software developers and data experts.

Using an array to insert data into a table.

Hello again

Need some help inserting data into a table using an array.

This works:
$col1 = 2;
$col2 = 2;
$col3 = 2;
$col4 = 2;
$query = "INSERT INTO test (a,b,c,d) VALUES ($col1,$col2,$col3,$col4)";

and this does not

$col = array(2,2,2,2);

$query = "INSERT INTO test (a,b,c,d) VALUES ($col)";

Regards
Dynamo

Jul 17 '05 #1
6 2602
.oO(Dynamo)
Need some help inserting data into a table using an array.

This works: [...]

and this does not

$col = array(2,2,2,2);

$query = "INSERT INTO test (a,b,c,d) VALUES ($col)";


Have a look at implode().

$query = sprintf('INSERT INTO test (a, b, c, d) VALUES (%s)',
implode(',', $col));

Micha
Jul 17 '05 #2
.oO(Michael Fesser)
Have a look at implode().

$query = sprintf('INSERT INTO test (a, b, c, d) VALUES (%s)',
implode(',', $col));


.... assuming that all the values are numeric, so they don't have to be
quoted.

Micha
Jul 17 '05 #3
On Mon, 22 Nov 2004 20:43:07 +0100, Michael Fesser <ne*****@gmx.net>
wrote:
.oO(Michael Fesser)
Have a look at implode().

$query = sprintf('INSERT INTO test (a, b, c, d) VALUES (%s)',
implode(',', $col));


... assuming that all the values are numeric, so they don't have to be
quoted.


If $col = "2, 3, 1, 4",

then %s would be:

2 3 1 4

no?
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Jul 17 '05 #4
.oO(Gary L. Burnore)
If $col = "2, 3, 1, 4",

then %s would be:

2 3 1 4

no?


No, why should it?

The OP wants $col to be an array:

$col = array(2, 2, 2, 2);

The result of implode(',', $col) will be the string '2,2,2,2'.

Whether you write

$query = 'INSERT INTO test (a, b, c, d) VALUES ('.implode(',',
$col).')';

or

$query = sprintf('INSERT INTO test (a, b, c, d) VALUES (%s)',
implode(',', $col));

makes no difference, the result will be the same.

Micha
Jul 17 '05 #5

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:n8********************************@4ax.com...
.oO(Michael Fesser)
... assuming that all the values are numeric, so they don't have to be
quoted.

And what if the values are text?

Regards
Dynamo
Jul 17 '05 #6
.oO(Dynamo)
"Michael Fesser" <ne*****@gmx.net> wrote
... assuming that all the values are numeric, so they don't have to be
quoted.

And what if the values are text?


Strings have to be single-quoted inside a query, so the simple implode()
won't work.

Micha
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Frank Maestas | last post by:
Hello, I database table that I need to add checkbox data (using an array) and standar fields. My standard fields insert just fine, as does my array when trying them alone. Together - doesn't...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
11
by: Chris Fink | last post by:
I have setup an Oracle table which contains a blob field. How do I insert data into this field using C# and ADO.net?
5
by: Stephen Preston | last post by:
Trying to load an multidimensional array into a MySQL table with columns as follows, level1,level2,level3,illust,item,description,partNo,qua,price,remarks,weight ,size,mass the array first...
22
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple'...
1
by: nel | last post by:
actually i want to insert for example : "insert in $table_name1 f$ield_name1, $field_name2, $field_name3 values $data1, $data2, $data3"; table & field variable i get from select statement form...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.