Solved

How do I loop these results?

Posted on 2015-02-20
6
48 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 42

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 42

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 42

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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.

744 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

15 Experts available now in Live!

Get 1:1 Help Now