Solved

multiple statements one connection pdo

Posted on 2014-10-09
6
327 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

862 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

24 Experts available now in Live!

Get 1:1 Help Now