Jump to content

first line not outputting, when reading from MySQL database


ScrewLooseSalad

Recommended Posts

My code seems to output a line short, I can't seem to make it output the first line, I've tried tinkering, but can't seem to get that first line out, can anyone point out what I'm doing wrong? Thanks

 


$queryitem = "SELECT * FROM `delnoteitems` WHERE `deliverNote` LIKE '$DeliverNote';";
$result = $db2->query($queryitem);
if(!$result) {echo "Query two of two failed<br>$queryitem"; exit(0);}
$num_results = $result->num_rows;
$row = $result->fetch_assoc();
$i=0;
while ($row = $result->fetch_assoc()) {
	$i++;
	$itemDesc[$i] = $row['itemDesc'];
	$itemQuantity[$i] = $row['itemQuantity'];
}
$max=$i;

I think I've spotted the problem. You increase $i before populating the arrays, which means $itemDesc[0] and $itemQuantity[0] are empty. So simply try changing your while to this:

while ($row = $result->fetch_assoc()) {
    $itemDesc[$i] = $row['itemDesc'];
    $itemQuantity[$i] = $row['itemQuantity'];
    $i++;
}

Now you'll start populating $itemDesc[0] and go up.

 

- W

I think I've spotted the problem. You increase $i before populating the arrays, which means $itemDesc[0] and $itemQuantity[0] are empty.

- W

 

 The $i is initiated at '0' and increases first so that $i is only incremented as many times as there are entries, in theory, so that only the amount of entries are printed later, as you can see $i is only used in the variable names that I'll be calling later on in the script. There is a problem here but I don't think this is the solution, I did try it, and two entries went missing, as the second entry disappeared into $i=0, so it wasn't called at the bottom of the script; I've tried calling $itemDesc[$i=0] before but of course nothing was in there, perhaps the problem could be where I call the variable?

 

$i=1;
while($i<=$max) {
	echo "<p align='center'>$itemDesc[$i]</p>";
	$i++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.