Solved

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

Posted on 2014-12-22
2
100 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

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 …

706 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

19 Experts available now in Live!

Get 1:1 Help Now