0

I guess my question is the opposite of most I have seen..

its not trouble with INSERTING data with apostrophes..its with pulling/displaying it.

Summary/Overview:

I use PDO to insert data/records into my MySQL database (table).. this put special characters/apostrophes..etc -in- the database (when looking at the data through MyPHPAdmin, I see the apostrophes in the cells/columns if applicable)

I later pull records from the table.. and save to an array.

code example so far:

//declare PDO DB connection
$table = 'target_table';
$conn=new PDO("mysql:host=localhost; dbname=test;","root","");

$query = "SELECT * FROM $table";
$tcstmt = $conn->prepare($query);
$tcstmt->execute();
$totalEntriesArray = array();
while($u = $tcstmt->fetch(PDO::FETCH_BOTH)){
    $totalEntriesArray[] = $u;
}

I populate a dropdown box.. and when the user makes a selection, I then have some js/jQuery to handle this event detection, grab the value from the dropdown box selection and my target index in the array, and populate some fields on the page.

example:

$("#dropdown").change(function(){
    $('#nameField input').val(totalEntriesArray[arrayIndex].target_name);
}

(with me so far? haha)

fairly easy/straight forward...and everything works fine, and as intended.

However, I noticed that if some of the text/data from the database that has apostrophes display in these fields, show up with the infamous 'black diamond/question mark' character..

So this isnt a situation where preventing SQL injection will fix it..or using PDO will solve it...etc. Its the opposite direction. :)

I need to somehow parse/format the data so it displays in the textfields properly.

What is the best way to achieve this?

thanks.

whispers
  • 962
  • 1
  • 22
  • 48

1 Answers1

0

grrr.. (isnt this always how it happens? you post.. and immediately fix things?) LOL

anyways... in an effort to give this thread some resolution for others..

I switched the character-set:

<!-- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -->
to
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

and it is now displaying fine.

whispers
  • 962
  • 1
  • 22
  • 48
  • what exactly is the silly step? Is there a better way to solve it? – whispers May 02 '14 at 02:51
  • the better - is to find out why you have the broken UTF-8 character. What is the code point for the character you name as an "apostrophe" – zerkms May 02 '14 at 02:54
  • The better way to do it is to edit your HTTP server's configuration file to output the content type as utf-8. – Ryan May 02 '14 at 02:55
  • @RPM: I'm 99% sure it has nothing to do with content-type in response. The content-type specified in a `` would behave exactly the same as if it was specified in http headers. – zerkms May 02 '14 at 02:57
  • I really didn't read the question, so my comment may have sounded like it suggested an answer to whatever his problem is. But it is easier to just have the content-type set on the server side rather than implementing meta tags like this. Some HTTP requests are not from browsers (curl, wget), so they don't parse HTML tags. – Ryan May 02 '14 at 02:59
  • @zerkms - I'm not 100% sure what your saying/asking? Code point? (not familiar with the term, sorry) The data is taken from form input, and using PDO/prepared statements to INSERT it into the table. (the raw data in the table looks like how it was typed/input into the form field) – whispers May 02 '14 at 03:05
  • @RPM - Currently, I'm testing all this from a local install of WAMP server. The 3rd post (bottom of it) recommended to change the character set.. I tried and it worked for me. http://stackoverflow.com/questions/275411/php-output-showing-little-black-diamonds-with-a-question-mark Unfortunately I dont fully understand the problem I guess? So the database/table was set-up incorrectly? Is that was your getting at? – whispers May 02 '14 at 03:07
  • @whispers: "not familiar with the term, sorry" --- google is familiar with it. But as soon as you don't care in the quality of the solution - just go with the current one. – zerkms May 04 '14 at 20:19