Page 1 of 1

Quotations inside a MySQL database

Posted: Sun Jul 20, 2008 9:21 pm
by jaandrws
How do I need to handle a query to a mysql database when I know the returned data may possibly contain quotation marks?
Code:
$query1 = "SELECT * FROM photo_log WHERE name = '$version'";
$result1 = mysql_query($query1) or die("Could not perform query: ".mysql_error());
$row1 = mysql_fetch_array($result1);

The returned data is cut off from the point of a quotation mark. THE DATA IS: ITHF's "The Buth" at 2007 Convention.
If I try add slashes after the query, I get: ITHF\'s \
I've tried a bunch of different approaches, but frankly this kind of problem has always confused me. My global_pc and global_runtime are both turned off. Sorry to be a bother! Thanks!

Re: Quotations inside a MySQL database

Posted: Sun Jul 20, 2008 11:42 pm
by Bill H
display the output using

Code: Select all

htmlentities(stripslashes($row1['field']),ENT_QUOTES)

Re: Quotations inside a MySQL database

Posted: Mon Jul 21, 2008 1:25 am
by devendra-m
If you encode string before save and decode after retrieval using base64 encoding, you will have no problem with string of any characters.

Re: Quotations inside a MySQL database

Posted: Mon Jul 21, 2008 4:23 am
by verymeanguy
I am very new to php but I think this will be very important to me in the future. So, how does one encode and decode a piece of string use base 64 encoding in php?

Thanks in advance!

Re: Quotations inside a MySQL database

Posted: Mon Jul 21, 2008 4:28 am
by devendra-m
base64_encode()
base64_ decode()

Re: Quotations inside a MySQL database

Posted: Mon Jul 21, 2008 4:34 am
by padduweb
jaandrws wrote:How do I need to handle a query to a mysql database when I know the returned data may possibly contain quotation marks?
Code:
$query1 = "SELECT * FROM photo_log WHERE name = '$version'";
$result1 = mysql_query($query1) or die("Could not perform query: ".mysql_error());
$row1 = mysql_fetch_array($result1);

The returned data is cut off from the point of a quotation mark. THE DATA IS: ITHF's "The Buth" at 2007 Convention.
If I try add slashes after the query, I get: ITHF\'s \
I've tried a bunch of different approaches, but frankly this kind of problem has always confused me. My global_pc and global_runtime are both turned off. Sorry to be a bother! Thanks!
use mysql_real_escape_string($str) function to escape values contains single quote or double quote etc.

For more PHP Answers

Re: Quotations inside a MySQL database

Posted: Mon Jul 21, 2008 4:36 am
by padduweb
use mysql_escape_string() function to escape all single quote and double quote values in string

For more PHP Answers