[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I loop these results?

Posted on 2015-02-20
6
Medium Priority
?
62 Views
Last Modified: 2015-02-21
How do I loop this quesry and place all of the COMPATIBLE PART numbers into one column (Maybe an ARRAY?)

CURRENT RESULTS: (via echo)

COMPATIBLE                MY
PART #                           PART #

02K6524                     02K6524
02K6525                     02K6524
02K6526                     02K6524
02K6631                     02K6524
02K6634                     02K6524
02K6635                     02K6524
84G2094                     02K6524
84G2144                     02K6524
84G2392                     02K6524
B-5627                         02K6524
IB360                           02K6524




DESIRED RESULT:  (DB Columns)

("CompatiblePartNumber")                                                                                                              
02K6525,  02K6524,  
02K6526, 02K663,  
02K6634, 02K6635,
84G2094, 84G2144,
84G2392, B-5627,
IB360 ,  etc .

("ERPartNumber")
02K6524


$query3 = $conn->query('SELECT DISTINCT CompatiblePartNumber, ERPartNumber FROM `ErepCrossReference1`');
    
	while ($row3 = $query3->fetch(PDO::FETCH_ASSOC))
    {
	
	
echo $row3["CompatiblePartNumber"]."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$row3["ERPartNumber"]."<BR>";


	// DATABASE INSERT IN PDO
	}

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 40622215
Not really sure there's enough info here to provide an accurate answer, as the records you get out of your database may differ. For example, will your query always return several CompatiblePartNumbers and only 1 ERPartNumber.

Basically, as you loop through the query results, build an array, and them implode the array to get a string value to insert into your database. Something like this should get you going, but if not, then we'll need more info:

$partNumbers = array();
while ($row3 = $query3->fetch(PDO::FETCH_ASSOC)) {
     // build an array of the part numbers
     $partNumbers[] = $row3["CompatiblePartNumber"];
     ...
}
// convert the array into a comma delimited string
$partNumberList = implode(",", $partNumbers);

// now insert that into your database
$stmt = $conn->prepare("INSERT INTO yourTable (CompatiblePartNumber) VALUES (?))");
$stmt->execute(array($partNumberList));

Open in new window

0
 

Author Comment

by:lawrence_dev
ID: 40622256
That is exactly what I was looking for!  I will work on it, and then close the question after I have made sure I have no more questions!   Thanks for your help!!!
0
 

Author Comment

by:lawrence_dev
ID: 40622401
I am getting an error on this line ==>  $stmt->execute(array($partNumberList));

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

How do I fix?

$partNumbers = array();
while ($row3 = $query3->fetch(PDO::FETCH_ASSOC)) {
     // build an array of the part numbers
     $partNumbers[] = $row3["CompatiblePartNumber"];
}
// convert the array into a comma delimited string
$partNumberList = implode(",", $partNumbers);

	
	
$stmt = $conn->prepare('INSERT INTO `testtable` (ERPartNumber, CompatiblePartNumber) VALUES(:ERPartNumber, :CompatiblePartNumber)');
$stmt->execute(array($partNumberList));	

                  $params = array(':ERPartNumber'=>isset($row3['ERPartNumber'])?$row3['ERPartNumber']:"",
		                      ':CompatiblePartNumber'=>isset($partNumberList)?$partNumberList:"",);
		            $statement->execute($params); 
	
	
	}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 40622417
Line 12 of your code is executing the query with one 1 parameter, but your query has 2. Delete that line. You then go on to set the parameters, but $row3 won't exist because it's outside of your while() loop. You then call execute() on the $statement object, but it should be $stmt. Finally, you appear to have an extra closing bracket at the end:

$partNumbers = array();
$erPartNumber = '';
while ($row3 = $query3->fetch(PDO::FETCH_ASSOC)) {
     // build an array of the part numbers
     $partNumbers[] = $row3["CompatiblePartNumber"];
     $erPartNumber = $row3["ERPartNumber"];
}

// convert the array into a comma delimited string
$partNumberList = implode(",", $partNumbers);

// prepare the query
$stmt = $conn->prepare('INSERT INTO `testtable` (ERPartNumber, CompatiblePartNumber) VALUES(:ERPartNumber, :CompatiblePartNumber)');

// bind the parameters
$params = array(':ERPartNumber' => $erPartNumber, ':CompatiblePartNumber' => isset($partNumberList) ? $partNumberList : '');

// execute the query
$stmt->execute($params); 

Open in new window

0
 

Author Comment

by:lawrence_dev
ID: 40622469
OK that worked great! However it only returned 1 row.  There are 11000+ lol.
THANK YOU for your help!  

Please advise if I need to ask a followup question.

How do I loop through each one?

	$query3 = $conn->query('SELECT DISTINCT CompatiblePartNumber, ERPartNumber FROM `ErepCrossReference1`');
    
// LOOP HERE???  		
$partNumbers = array();
$erPartNumber = '';
while ($row3 = $query3->fetch(PDO::FETCH_ASSOC)) {
     // build an array of the part numbers
     $partNumbers[] = $row3["CompatiblePartNumber"];
     $erPartNumber = $row3["ERPartNumber"];
}

// convert the array into a comma delimited string
$partNumberList = implode(",", $partNumbers);

// prepare the query
$stmt = $conn->prepare('INSERT INTO `testtable` (ERPartNumber, CompatiblePartNumber) VALUES(:ERPartNumber, :CompatiblePartNumber)');

// bind the parameters
$params = array(':ERPartNumber' => $erPartNumber, ':CompatiblePartNumber' => isset($partNumberList) ? $partNumberList : '');

// execute the query
$stmt->execute($params); 
	
	

Open in new window

0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 40622834
If only one record is being returned then that's what your query is returning. You're already looping through the query results, so maybe your query is wrong. In your original question, the results you showed only contained several records, all with the same ERPartNumber.

You may have 11000 records in your database, but your Query is asking for DISTINCT records. Maybe that's the issue. In my first question, I said that your records only showed 1 ERPartNumber, and you replied that it was exactly what you were looking for.

Without know your database, we can't really offer advice on the query yo need to run.

You may want to open another question if the query is not behaving as you expect
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

650 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