refiking Posted October 3, 2008 Share Posted October 3, 2008 I can store apostrophes with no problem. But when I call them from the db in a query, it screws it up. How should I handle it so it can actually read the record from the database and echo the record with the apostrophe in tact? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/ Share on other sites More sharing options...
Porl123 Posted October 3, 2008 Share Posted October 3, 2008 You should be adding slashes before you enter them into the database, then you can strip them on the other side Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656117 Share on other sites More sharing options...
refiking Posted October 3, 2008 Author Share Posted October 3, 2008 Can you givve me an example please? Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656118 Share on other sites More sharing options...
Porl123 Posted October 3, 2008 Share Posted October 3, 2008 $text = "Steve's text"; $string = addslashes($text); mysql_query("INSERT INTO table (`text`) VALUES ('".string."')"); Then to retrieve $query = mysql_query("SELECT text FROM table WHERE ....."); $text = mysql_fetch_array($query); $text = stripslashes($text[0]); echo $text; Something like that Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656119 Share on other sites More sharing options...
refiking Posted October 3, 2008 Author Share Posted October 3, 2008 OK. So, what do I do about having more than 1 record with a possible apostrophe? I can't use string twice when entering in the db, right? Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656120 Share on other sites More sharing options...
Porl123 Posted October 3, 2008 Share Posted October 3, 2008 Any string that goes into the database can have those functions as many times as you want Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656121 Share on other sites More sharing options...
Porl123 Posted October 3, 2008 Share Posted October 3, 2008 Infact instead of using addslashes before it goes into the database, I'd increase the security and use function clean($data) { if (!get_magic_quotes_gpc()) { $data = addslashes($data); } $data = strip_tags($data); $data = htmlspecialchars($data, ENT_QUOTES); $data = trim($data); return $data; } It's a bit more safe Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656122 Share on other sites More sharing options...
refiking Posted October 3, 2008 Author Share Posted October 3, 2008 OK. So, I add this function and call my variables. Then, how would I use this function with those variables. For example: [code] while($row = mysql_fetch_assoc($sql)){ $var1 = $row['field1']; $var2 = $row['field2']; $var3 = $row['field3']; } //Would I then add this? // $var1 = clean($var1); $var2 = clean($var2); $var3 = clean($var3); [/code] Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656126 Share on other sites More sharing options...
kenrbnsn Posted October 3, 2008 Share Posted October 3, 2008 Actually, you should used the mysql_real_escape_string() function, not addslashes(), since it handles all potential problem characters, not just the single quote. Also, stripslashes() and htmlentities() should be used when you display stored values, not when you store them, Ken Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656129 Share on other sites More sharing options...
refiking Posted October 3, 2008 Author Share Posted October 3, 2008 OK, so would I use the mysql_real_escape_string() function when displaying or storing the value? Thanks in advance for being patient with a noob. Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656132 Share on other sites More sharing options...
schilly Posted October 3, 2008 Share Posted October 3, 2008 mysql_real_escape_string() when you store. Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656137 Share on other sites More sharing options...
refiking Posted October 3, 2008 Author Share Posted October 3, 2008 OK. So, when I display the variable, will I have to add any kind of function or anything? Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656141 Share on other sites More sharing options...
schilly Posted October 3, 2008 Share Posted October 3, 2008 stripslashes() and htmlentities() as per Ken ex. stripslashes(htmlentities($var)) Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656143 Share on other sites More sharing options...
refiking Posted October 3, 2008 Author Share Posted October 3, 2008 OK. SO, I think I got it now. Let me know if this is it. while($row = mysql_fetch_assoc($sql)){ $var1 = $row['field1']; $var2 = $row['field2']; } $var1 = stripslashes(htmlentities($var1)); $var2 = stripslashes(htmlentities($var2)); Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656144 Share on other sites More sharing options...
schilly Posted October 3, 2008 Share Posted October 3, 2008 Looks good. Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656146 Share on other sites More sharing options...
refiking Posted October 3, 2008 Author Share Posted October 3, 2008 Ok. Thanks! Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656147 Share on other sites More sharing options...
kenrbnsn Posted October 3, 2008 Share Posted October 3, 2008 You should use stripslashes() before htmlentities(): <?php while($row = mysql_fetch_assoc($sql)){ $var1 = htmlentities(stripslashes($row['field1']),ENT_QUOTES); $var2 = htmlentities(stripslashes($row['field2']),ENT_QUOTES); // // do something with var1 & var2 // }?> Ken Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656261 Share on other sites More sharing options...
nadeemshafi9 Posted October 3, 2008 Share Posted October 3, 2008 depends when you say screws up what does that mean, mysql adds slashes for you and also removes them on retrival Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656263 Share on other sites More sharing options...
nadeemshafi9 Posted October 3, 2008 Share Posted October 3, 2008 how to handel apostraphees, you hang behead or quarter them Link to comment https://forums.phpfreaks.com/topic/126854-how-to-handle-apostrophes/#findComment-656373 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.