Jump to content

first line not outputting, when reading from MySQL database


ScrewLooseSalad
Go to solution Solved by davidannis,

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;

Edited by ScrewLooseSalad
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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++;
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.