Solved

multiple statements one connection pdo

Posted on 2014-10-09
6
320 Views
Last Modified: 2014-10-10
Hi

How do you send multiple statements on one command in pdo

Ihave seent this

http://stackoverflow.com/questions/6346674/pdo-support-for-multiple-queries-pdo-mysql-pdo-mysqlnd

But the tone suggets you shouldnt do it

I have seen mysqli_multiquery

Is there any reason why you shouldnt send mltiple statements in one command in pdp to mysql? Just trying to exany my knowledge
0
Comment
Question by:andieje
6 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 167 total points
ID: 40370213
One of the reasons can be that what if you send three statements related to each other insert in the first then update of the inserted record in the second line and the insert fails and the update is successful you can then end up with some dirty data and the tracking of that is quite a mess.

Also, the security threat, since like this some may alter your site and send in the drop table or database statement etc..

My opinion, more the issues then the benefits but if it suits you just take care of those things and go with it...
0
 

Author Comment

by:andieje
ID: 40370276
ok

1)scenario 1
Make connection
Make repeaed statements on on go
close connection

2)secenrio 2
make db connection
make multiple statements
disconnect

In my case the statements are just imple retrieval,

I dont understand your last sentence. My apologies. Please could you explain. Are you saying more issues sending multiple statements in one go than the benefits?

I can follow that advice.

But you only need to open the database connection once dont you for all your statements? Or do you have to open/close each time to free it up for other users as that seems to be taking time
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40370290
Basically,

you open connection for sql and close it but here the difference is that in that sql you have multiple SQL statements instead of just one in the first case...

What i meant is that there can be some possible issues in using it than benefits and that you should test it regarding the performances of the database if there is any change and you are ok with it you use if not try not to use it...

Anyway, the best way is that you try and review performance of both approach and then decide...

And no need to apology for not understanding, just ask;)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 167 total points
ID: 40370298
Database connections only need to be opened once. You then use the connection to run whatever queries you wish and then close it when you are done.

As for the advantages of packing multiple queries into one statement and running it ... well I cannot see any. I understand that you are running SELECT queries and therefore it may not be much of an issue but how do you check for success of all queries and if you are ever in a situation where query #2 depends on query #1 what happens then?

I run one query at a time and then check its outcome before doing the next so these issues never arise for me. It seems to me that throwing a lot of queries at the database and then picking through the outcomes trying to determine what happened would require a lot of work and careful programming.

At the end of the day you have a choice and it is your decision which to use, but personally I cannot see any advantages of single-statement-multiqueries.
0
 

Author Comment

by:andieje
ID: 40373752
I've requested that this question be closed as follows:

Accepted answer: 0 points for andieje's comment #a40370276
Assisted answer: 250 points for samo4fun's comment #a40370213
Assisted answer: 250 points for samo4fun's comment #a40370290

for the following reason:

brilliant
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40370510
What @bportlock said.  Of course, there are transactions, but that seems to be beyond the scope of this question.  If you're unsure about how to use MySQL databases with PHP, take a little while with this article.  It shows all the basics.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now