Solved

How do I loop these results?

Posted on 2015-02-20
6
56 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 43

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 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 43

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

717 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