warnings - Undefined offset error while printing values from db in php -
undefined offset error occurs while printing values of mysqli_fetch_array() using for loop:
code:
$con=mysqli_connect($serverid,$useid,$passwordid,$dbid);<br> $fieldcheck= "select * userregistrationform";<br> $execfield=mysqli_query($con,$fieldcheck);<br> if(mysqli_num_rows($execfield)>0) { $idcheck= "select changeid userregistrationform"; $execq0=mysqli_query($con,$idcheck); $idcmp=mysqli_fetch_array($execq0); $idlength=count($idcmp); echo $idlength; for($x=0;$x < $idlength;$x++) { echo $idcmp[$x]; } note:
- changeid column in database.
the code worksfine when use
while($idcmp=mysqli_fetch_array($execq0)) { echo $idcmp['changeid']; }
but helpful why error occurs if use loop.
when print $idcmp see like:
array('changeid' => 100) see, key in array changeid.
when
for($x=0;$x < $idlength;$x++) { echo $idcmp[$x]; } you try access key value 0. there's no key 0 in $idcmp. that's why offset error.
Comments
Post a Comment