PHP Not Handling Query Result As I'd Expected And I Cannot See Why

Thank you for looking at my question,

I have a php page that when it opens runs a query against a MySQL table - currently not a huge table, contains three rows of data, and writes the data to an array.
	$queryAssembly = "SELECT ProductionOrderTube, Meter, TubeAssembly, WiredInner FROM tblTubeAssembly_Base ORDER BY ProductionOrderTube DESC";
	$resultAssembly = $conn->query($queryAssembly);
	if (!$resultAssembly) die($conn->error);
	
	$rowsAssembly = $resultAssembly->num_rows;

	for($i=0; $i<$rowsAssembly; $i++){
		$AssemblyArray[$i][0] = $resultAssembly->fetch_Assoc()['ProductionOrderTube'];
		$AssemblyArray[$i][1] = $resultAssembly->fetch_Assoc()['Meter'];
		$AssemblyArray[$i][2] = $resultAssembly->fetch_Assoc()['TubeAssembly'];
		$AssemblyArray[$i][3] = $resultAssembly->fetch_Assoc()['WiredInner'];
	}

Open in new window


When I display the results to the screen using:
	echo '<table border="1">';
		echo '<tr>';
			echo '<td>$i</td><td>Prodn Order</td><td>Meter</td><td>Tube Ass</td><td>Wired Inner</td>';
		echo '</tr>';
		
		for($i=0; $i<$rowsAssembly; $i++){
			echo '<tr>';
				echo '<td>' .$i. '</td>';
				echo '<td>' .$i. ' ... ' .$AssemblyArray[$i][0]. '</td>';
				echo '<td>' .$i. ' ... ' .$AssemblyArray[$i][1]. '</td>';
				echo '<td>' .$i. ' ... ' .$AssemblyArray[$i][2]. '</td>';
				echo '<td>' .$i. ' ... ' .$AssemblyArray[$i][3]. '</td>';
			echo '</tr>';
		}
	echo '</table>';

Open in new window

Instead of getting three rows of four elements, the first row of the display table contains the Prodn Order number from the first row of the SQL table, the meter from the second row of the sql table, the tube assembly from the third row and, because there isn't a fourth row, wired inner = 0.

The next two rows of the display table contain zeroes


I can't see what is causing this to happen. Please, somebody, point out what I've missed
Gary CroxfordOperations Support AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Each time you call fetch_assoc, you move to the next record which is why you have issues. Unless you specifically need to put the data into an array, you can loop through it like so:

while($row = $resultAssembly->fetch_assoc()):
    ...
    echo '<td>' . $row['ProductionOrderTube'] .'</td>';
    ...
endwhile;

Open in new window

If you really do want to put it in an array, then try this:

while($row = $resultAssembly->fetch_assoc()):
    $AssemblyArray[] = $row;
endwhile;

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
Actually, if you want it all in an array, you can bypass the loop altogether:

$AssemblyArray = $resultAssembly->fetch_all(MYSQLI_ASSOC);
0
Gary CroxfordOperations Support AnalystAuthor Commented:
Thank you for your help, I've been looking at this all morning.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.