AllExperts > PHP5 
Search      
PHP5
Volunteer
Answers to thousands of questions
 Home · More PHP5 Questions · Answer Library  · Encyclopedia ·
More PHP5 Answers
Question Library

Ask a question about PHP5
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Kevin 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.

Education/Credentials
BS - IT/CS

 
   

You are here:  Experts > Computing/Technology > Perl/PHP > PHP5 > Output apostrophes, quotation arks etc from Mysql database

PHP5 - Output apostrophes, quotation arks etc from Mysql database


Expert: Kevin Cackler - 10/14/2009

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.

I have included a code example below;

printf("<td><p><img src=\"$ABSOLUTE_PATH_1%s\" align=\"center\"</p><p class='heading2'>%s</p><p>%.20s<br><a href=\"%s?id=%s\">  (MORE)</a><br></p></td>", $row["image_1"], $row["project"], $row["company"],$PHP_SELF, $row["id"]);

echo ("<img src='$ABSOLUTE_PATH_1$row[2]' hspace=\"5\" vspace=\"5\" align='left' border =\"1\"><h2>$row[4]</h2>$row[5]");

How do I do this?

Many thanks

ANSWER: I apologize, but it's very difficult to understand what you're trying to do, still.  

However, if I've understood your question properly, what you want is concatenation.  Your line can be changed to look like this:



echo '<img src="' . $ABSOLUTE_PATH_1 . $row[2] . '" hspace="5" vspace="5" align="left" border ="1"><h2>' . $row[4] . '</h2>' . $row[5];

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.

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.