简体   繁体   中英

MySQL/PHP puts an enter and whitespace after record echo

I got the following code:

if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
      echo'<img src="'.$row['url'].'"/>';
  }
}

The following outputs the row URL with a whitespace and a linebreak after it.

src="upload/imgurl.JPG <-- (Here is a space and on the next line the ")
"

When I manually type in a URL manually it all works fine. Also the URLs in the database are without space, breakline, etc.

Why does PHP/MySQL put this break and space after my string? How could I solve it without hardcoding a trim or something like that?

It would be good to use trim() function before adding data in the db. Now you can also trim() the output-data. Its not a big deal at all

Trim — Strip whitespace (or other characters) from the beginning and end of a string. Here:

if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
      $trimmed = trim($row['url']);
      echo'<img src="'.$trimmed.'"/>';
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM