AboutKevin Cackler Expertise Any and everything related to PHP4 and PHP5. I specialize in functional, readable, scalable object oriented code, and can answer your troublesome class and object questions.
Experience 5 years developing in PHP using flat files and databases (MySQL, Oracle)
Lead PHP developer for a very large Texas based web hosting company
The coder behind some of the largest pet communities online.
Question QUESTION: I need to display mysql data with php 4 or 5 that has apostrophes etc. within the text. The data is read directly from the database and displayed within strings using echo and printf statements.
In case you aren't able to tell, concatenation involves stopping the parsing of your text (IE, if you started with echo ', then to stop termination you place a '), then add a . then add your variable then add a . and then start up the parsing again with your opening quote '
A simpler example
<?php
$var='Kevin';
echo $var . ' Cackler';
echo 'Cackler, ' . $var;
?>
---------- FOLLOW-UP ----------
QUESTION: My mistake, I was not clear with my question...it is this;
In the database in a field called title the data is Fred's bakery.
When that string is output I get Freds bakery or spaces in some browsers. I think I know the problem...it is getting php to print the apostrophe.
It must be using addslashes() stripslashes() or mysql_real_escape_string() at some point. I am not sure where and sent the sample for you to suggest placement in my example.
By the way your answer on concatenation answered another problem.
Many thanks
Answer If the value actually stored in the database field is Fred's Bakery (You've made sure that that apostrophe actually exists in the field, correct?), then when you output $row[2] or whatever, it should print that apostrophe...Period.
addslashes() is used to add a backslash (\) before quotes (') in order to prevent DB issues
stripslashes() is used to remove those extra backslashes when echoing the value to the screen
mysql_real_escape_string() does the same thing as addslashes, plus so much more, and is the preferred method to escape database input.
Regardless which of those you use, however, if the value in the database contains an apostrophe and you print that value to the screen without modifying it in any way, then that apostrophe should still be there.
If you are indeed missing apostrophes, then something is happening to your data before you are echoing it. It definitely is not normal PHP behavior, however.