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?
 
Chris StanyonCommented:
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
 
Chris StanyonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.