Solved

Transitioning to PDO/MySQL.  Need help  with two INSERT statements on one query

Posted on 2014-12-22
2
101 Views
Last Modified: 2014-12-22
Is it possible to have two insert statements on one query with PDO?  (Need one for Level 0 and one for level 1 as shown below)

$query6 = $conn->query('SELECT DISTINCT catidexp1 FROM download');

$statement = $conn->prepare('INSERT IGNORE INTO category_path (category_id,path_id,level) VALUES(:catid,:pathid,:level)');

$statement2 = $conn->prepare('INSERT IGNORE INTO category_path (category_id,path_id,level) VALUES(:catid,:pathid,:level)');

while ($row6 = $query6->fetch(PDO::FETCH_ASSOC)){
	
		$params = array(':catid'=>$row6['catidexp1'],':pathid'=>$row6['catidexp1'],':level'=>"0");
		$statement->execute($params); 

$params = array(':catid'=>$row6['catidexp1'],':pathid'=>$row6['catidexp1'],':level'=>"1");
		$statement->execute($params); 
	
}

Open in new window

0
Comment
Question by:lawrence_dev
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40513489
Yes, you only need the one statement as they are doing the same thing

$query6 = $conn->query('SELECT DISTINCT catidexp1 FROM download');

$statement = $conn->prepare('INSERT IGNORE INTO category_path (category_id,path_id,level) VALUES(:catid,:pathid,:level)');

while ($row6 = $query6->fetch(PDO::FETCH_ASSOC)){
	$params = array(':catid'=>$row6['catidexp1'],':pathid'=>$row6['catidexp1'],':level'=>"0");
	$statement->execute($params); 

	$params = array(':catid'=>$row6['catidexp1'],':pathid'=>$row6['catidexp1'],':level'=>"1");
	$statement->execute($params); 
}

Open in new window

0
 

Author Closing Comment

by:lawrence_dev
ID: 40513763
Thanks for your help Gary!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
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…

867 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

18 Experts available now in Live!

Get 1:1 Help Now