Mysqli_fetch_array for php variable variable

I am uploading 4 photos to a folder and the names of the photos to mysql. Then I "CHOOSE" the filenames and place them in the picArray as shown. But when I try to iterate over the contents, although I am assuming the correct names, it seems that they cannot be used as a directory path.

Here is the code:

                $qr = mysqli_query($con,"SELECT * FROM photos WHERE report='$repId'");

                $picArray = Array();

                while( $row = mysqli_fetch_array($qr) ) {
                    $picArray[] = $row['name'];


                <img  id="img1" src="uploads/<?php echo $repId ?>/<?php echo $picArray[0] ?>" />
                <img  id="img2" src="uploads/<?php echo $repId ?>/<?php echo $picArray[1] ?>" />
                <img  id="img3" src="uploads/<?php echo $repId ?>/<?php echo $picArray[2] ?>"  />
                <img  id="img4" src="uploads/<?php echo $repId ?>/<?php echo $picArray[3] ?>"  />


What I should mention is that when i:

echo $picArray[0];
echo $picArray[1];
echo $picArray[2];
echo $picArray[3];


I get:

rafiki.png rafiki2.png


which means the array has a newline and spaces on it ... Why is this happening?


source to share

1 answer

There are probably new lines stored with the image names in the database, this is a fix, but you have to clear the data.

 while( $row = mysqli_fetch_array($qr) ) {
                    $picArray[] = trim($row['name']);




All Articles