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:

                $con=mysqli_connect("localhost","root","mypass","fixit");
                $con->set_charset("utf8");
                $repId=$repId+1;
                $qr = mysqli_query($con,"SELECT * FROM photos WHERE report='$repId'");


                $picArray = Array();

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

        ?>      

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

      

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
rafiki3.pngrafiki4.png 

      

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

+3


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']);
                }

      

+4


source







All Articles