473,224 Members | 1,942 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,224 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 2598
.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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.