2

I have this code:

<?php
   $conn = mysqli_connect("localhost", $db['username'], $db['password'], "the_table");

   // Check connection
   if (!$conn) {
       die("Connection failed: " . mysqli_connect_error());
   }

   $sql = "SELECT * FROM xlsws_images";

   if(!$result = $conn->query($sql)){
       die('There was an error running the query [' . $db->error . ']');
   }

   while($row = $result->fetch_assoc()){
       $path = $row['image_path'];
       echo $path;
   }
?>

It works as expected until it reaches a row with an image_path that has double quotes in it such as product/1/1”-wood-wheel-casters-set-of-4.png. In this case it returns the path without the double quotes in it like so product/1/1-wood-wheel-casters-set-of-4.png.

What makes these quotes disappear and how can I avoid that?

Joshua Goossen
  • 1,714
  • 1
  • 17
  • 36
  • It seems that my answer hasn't provided you with a solution. You also did not comment on it, so I'm not sure where we stand with this. I've made an edit to it and tested this. Do go over it again if you haven't seen the edits. Plus, you would need to tell us what that quote actually is. Ideally, you'd be best to take care of this during the file uploading and get rid of characters as such right away. – Funk Forty Niner Jan 28 '16 at 15:30
  • Use the console to run the query for that problematic result and tell us if it has the quote inside. The problem may be coming from when the data goes into the database, not coming out. – coladict Jan 28 '16 at 15:46

1 Answers1

4

"...that has double quotes in it such as product/1/1”"

That's a curly (smart) quote and should be changed to ". (That's if what you shown us in your question, is the actual type of quotes in your database).

That's why it's failing you and was introduced into your DB from the start.

You could use REPLACE

to change them in your table. (or even delete them).

Ideally, you should be taking care of getting rid of that quote before it gets to db.


Edit:

The following during testing, echo'd product/1/1”-wood-wheel-casters-set-of-4.png

$result = $connection->query("SELECT * FROM table");

while($row = $result->fetch_object()){

   echo $row->path_column;

}

So, it's hard to say what is causing your code to fail.

A few things you can try are:

$path = mysqli_real_escape_string($conn, $row['image_path']);

and

$path = htmlentities(stripslashes($row['image_path']));

Example of using str_replace():

$str = "product/1/1”-wood-wheel-casters-set-of-4.png";

echo $newstring = str_replace("”", "", $str);

Which produced:

product/1/1-wood-wheel-casters-set-of-4.png

  • If you wish to go that route in regards to file uploads.

Consult the following Q&A on Stack to replace characters (rename) during uploading files:

Sidenote: It was meant to replace spaces with underscores, but you can base yourself on that same logic.


A few more examples that could be useful.

This gets rid of the quote:

$result = $conn->query("SELECT * FROM table");

while($row = $result->fetch_object()){

    $new = $row->path_column;
    $newstring = str_replace("”", "", $new);

}

echo $newstring;

and escaping the newly changed curly quote to a regular quote:

$result = $conn->query("SELECT * FROM table");

while($row = $result->fetch_object()){

    $new = $row->path_column;
    $newstring = str_replace("”", "\"", $new);

}

echo $newstring;

which that one produced product/1/1"-wood-wheel-casters-set-of-4.png

  • Notice the " instead of the smart quote .

Edit:

As it turns out, it was a matter of passing UTF-8 before the connection was set.

OP: "Great! $conn->set_charset('utf8mb4'); worked. That was all I needed to added to my code above. Works seamlessly now. Thank you! – Joshua Goossen".

As per my comment to the OP:

"You can also try setting as UTF-8 as your connection before querying. Have a look at this Q&A on Stack UTF-8 all the way through there are a few answers in there. The connection thing is $connection->set_charset('utf8mb4'); as an example and used just before you open the db connection."

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Your answer is very informative. I am still working on it, with your suggestions in mind. I have no control of the quotes being put into the database (third party code), so right now I'm hoping `mysql_real_escape_string()` will help me – Joshua Goossen Jan 28 '16 at 21:16
  • @JoshuaGoossen Let's hope that something positive comes of it. It's too bad you didn't have control over it. Catching those quotes before uploading would definitely solve it right away. – Funk Forty Niner Jan 28 '16 at 21:23
  • I just tried `mysqli_real_escape_string($conn, $row['image_path'])` and `htmlentities(stripslashes($row['image_path']))` to no avail... – Joshua Goossen Jan 28 '16 at 21:28
  • 1
    @JoshuaGoossen Try `$path = $row['image_path'];` and after that line, add `htmlentities($path, ENT_COMPAT, 'utf-8')` then echo. You can also try setting as UTF-8 as your connection before querying. Have a look at this Q&A on Stack http://stackoverflow.com/q/279170/ there are a few answers in there. The connection thing is `$connection->set_charset('utf8mb4');` as an example and used just before you open the db connection. After this, I'm out of ideas. or `htmlspecialchars()` but am not 100% sure about that. – Funk Forty Niner Jan 28 '16 at 22:00
  • @JoshuaGoossen See also this Q&A http://stackoverflow.com/q/3382331/ - that's about it for what I can think of and found in regards to the problem. – Funk Forty Niner Jan 28 '16 at 22:03
  • 1
    Great! `$conn->set_charset('utf8mb4');` worked. That was all I needed to added to my code above. Works seamlessly now. Thank you! – Joshua Goossen Jan 29 '16 at 15:58
  • @JoshuaGoossen *AAaahhhhh, lovely!!!* Glad to hear it Joshua and thanks for the update, *cheers!* – Funk Forty Niner Jan 29 '16 at 15:59
  • @JoshuaGoossen Oh, and you're most welcome Joshua :-) I was glad to help and to finally see a solution come of this. – Funk Forty Niner Jan 29 '16 at 16:10