Reputation: 29
The array it is fetching has size 2 , so if we comment out $size
it returns 2 , but the for loop prints only the value present at 0th position of table column and not the next at 2th giving the error:
Undefined offset: 1
Code:
$allunstitched="Select p_id from unstitchedproduct";
$resultAllUnstitched= mysqli_query($connection,$allunstitched);
$AllUnstitchedResult= mysqli_fetch_array($resultAllUnstitched);
//$size=count($AllUnstitchedResult);
for ($i=0, $count = count($AllUnstitchedResult); $i < $count; ++$i) {
print $AllUnstitchedResult[$i];
}
Upvotes: 0
Views: 268
Reputation:
Um, i dont think you have a problem in your for-loop; since your query reads ONE field/column it should have 1 iteration (=0 index), so the =1 undefined is correct (in my opinion).
What you should do is a loop that calls repeatedly mysqli_fetch_array
to get all rows, like this:
while($AllUnstitchedResult = mysqli_fetch_array($resultAllUnstitched,MYSQLI_NUM)) {
foreach ($AllUnstitchedResult as $field) {
print $field;
}
}
Upvotes: 0
Reputation: 111279
You're using mysqli_fetch_array. This function returns the result twice by default: indexed both by number and by column name. Here the result set has only one column, so you get its value twice in the array: first with index 0 and then with a string index. There is no index 1.
To get the results with numerical indexes only, pass MYSQLI_NUM as a second argument to mysqli_fetch_array:
mysqli_fetch_array($resultAllUnstitched, MYSQLI_NUM);
Upvotes: 1
Reputation: 25435
If you start at 0, if count = 5 you'll have 6 elements (0,1,2,3,4,5), beware of that. You need to loop (count-1
) times:
$count = count($AllUnstitchedResult);
for ($i=0, $i < $count-1; $i++) {
print $AllUnstitchedResult[$i];
}
Upvotes: 0