473,748 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to send variable number of arguments to mysql execute()

fungazid
4 New Member
Help help help please

I’m using DBD::mysql, and I want to insert a record into clients table (id, address, and phone-number of a client):


Expand|Select|Wrap|Line Numbers
  1. my $str= “?,?,?,,,”; 
  2. my @args1=('Zalman','Zalmanovich',12121,'New York');
  3.  
  4. $q=$dbh->prepare("INSERT INTO $table VALUES($str)") 
  5.          or die "Couldn't prepare statement: " . $dbh->errstr; 
  6.  
  7. $q->execute($args1[0],$args1[1],$args1[2],…) 
  8.         or die "Couldn't execute statement: " . $q->errstr; 
  9.  
THE PROBLEM:
If I want to change the number of arguments sent to execute(), I must change the source code inside execute() brackets.
I would like to be able to change it on run-time with something like:

Expand|Select|Wrap|Line Numbers
  1. $q->execute(f(@args1)) 
  2.       or die "Couldn't execute statement: " . $q->errstr; 
  3.  
where f() formats the input to the function, so @args1 is allowed to have a variable size.
This may add invaluable flexibility to my program, because my program works with different kinds of clients tables, but how to do it ????.


thanks for your advices
Dec 18 '07 #1
2 2035
eWish
971 Recognized Expert Contributor
If I understand you correctly, you are using placeholders and you want to have more or less arguments and you want your code to adapt to it automatically which placeholder dosen't do real well. Is that correct?

--Kevin
Dec 19 '07 #2
eWish
971 Recognized Expert Contributor
You can do something like this, then fine tune it for your needs.
Expand|Select|Wrap|Line Numbers
  1. my @array = ();
  2.  
  3. my $id = $q->param('id') || undef;
  4. my $address = $q->param('address') || undef;
  5. my $phone = $q->param('phone') || undef;
  6.  
  7.    push(@array, $id,$address,$phone);
  8.  
  9. my $count = @array;
  10. my $placeholders = join(',', (split//, '?' x $count));
  11.  
  12. my $insert_statement = $dbh->prepare(qq{INSERT INTO $table_name(id,address,phone) VALUES($placeholders)});
  13.    $insert_statement->execute(@array);
  14.    $insert_statement->finish();
--Kevin
Dec 20 '07 #3

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

Similar topics

2
7920
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg' list should *automatically* be terminated with NULL past the user's last argument: http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdarg.html However, I (for some reason) have to pass in the count of the number of arguments, or manually...
2
2484
by: news.cablevision.qc.ca | last post by:
Hi! I want, using a form, enter a number and then, read that number of records in a database and store information in variables. The only way I see to do this is to do a FOR...NEXT to the number entered in the form, and then include the database reading into it. THE PROBLEM: <%
7
6297
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
134
7900
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
23
19202
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
4341
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call that function (using its pointer) and call it with the same argument list that was passed into the static method.
10
19752
by: CJM | last post by:
I'm trying to call a package/procedure in oracle (from an ASP page) which requires a number of parameters. I have got this working using OO40 but unfortunately the transaction rollback function doesnt seem to do much. So I'm now trying to use ADO instead (in the hope that ADO transactions will work), however I'm getting the above error. My initial searches havent turned up any suitable suggestions - there appear to be many reasons why...
4
1384
by: baccaril | last post by:
Hello, I have a form the post to a php script, the variable is passed to the php script successfully. I then query MySQL and output 8 rows at a time. So the problem I'm having is the first 8 rows are displayed on the web page correctly, but now when I attempt to display rows 9-16 the variable that was posted from the form is now blank. How do I ensure the variable does not disappear? LouB.
2
1922
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of numbers (doubles), and the functions only work in one dimension. I would ideally like to extend them to work in N dimensions, which means I want to pass some undefined number of pairs of arguments. I will loop through the N arguments to do the...
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9561
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, 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...
0
9381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
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,...
0
9254
tracyyun
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...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3316
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
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.