Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_fetch_array to php variable trash

Tags:

arrays

php

mysql

I upload 4 photos in a folder, and the names of the photos on mysql. Then i "SELECT" the names of the files, and put them in the array picArray as shown. But when i try to echo the contents, although i take the right names, it seem that they cant 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 that the array has newline and spaces on it... Why is this happening?

like image 574
Yakoumis Avatar asked Mar 09 '26 11:03

Yakoumis


1 Answers

There are probably new lines stored with the picture names in the database this is a fix but you should clean up you data

 while( $row = mysqli_fetch_array($qr) ) {
                    $picArray[] = trim($row['name']);
                }
like image 156
Jelle Keizer Avatar answered Mar 12 '26 02:03

Jelle Keizer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!