Solved

How do I loop these results?

Posted on 2015-02-20
6
49 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

896 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

17 Experts available now in Live!

Get 1:1 Help Now