Solved

How do I loop these results?

Posted on 2015-02-20
6
52 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

861 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