Archived

This topic is now archived and is closed to further replies.

PHP and mySQL problem

This topic is 5239 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does mySQL for some reason remove all ''\'' characters, or is my code at fault? I''m trying to store .DOC documents in a mySQL database. When I upload a document, it looks just fine when viewed as pure ASCII data. When I download it, it looks exactly the same - but without the ''\'' characters. These are the functions I use to add and download documents:
function dbAddDocument($name, $body, $username, $pass)
{
	if(!ValidateLogin($username, $pass) || !IsAdmin($username))
		return -1;
	$query = "INSERT INTO documents VALUES('''',''$name'',''$body'')";
	return mysql_query($query);
}

function dbGetDocumentByName($name)
{
	$query = "SELECT * FROM documents WHERE szName=''$name''";
	$result = mysql_query($query);
	if(!mysql_numrows($result))
		return "Could not find page, kthxdie!";
	return mysql_result($result, 0, "szBody");
} 

"THREE FOOT TELESCOPING DEMON PENIS"
- Pouya

Share this post


Link to post
Share on other sites
it sounds like the db is treating those slashes as escape characters (like \n and \r). do you notice of the character directly after the slash is missing as well. you may have to do a little string manipulation before you store the data in the db.

Basically, in your dbAddDcoument, do a global string replace on the $body

$body=str_replace(''\'',''\\'',$body);

make sure you use the single quotes, because a string in single quotes will not escape any characters. If you do want to use double quotes, you will have to double the number of slashes

str_replace("\\","\\\\",$body);


don''t know if this will work, but it''s worth a try.


Do you use your powers for good or for awesome?
My newly updated site | The Cutter Project | Association of Computing Machinery

Share this post


Link to post
Share on other sites
Along with what''s already been suggested, you could always use the addslashes() function. Does exactly what it says on the tin. ;]

--hellz

Share this post


Link to post
Share on other sites
I tried $body = str_replace("\\", "\\\\", $body), and now it doesn''t even want to insert it into the table...

mySQL hates me!


"THREE FOOT TELESCOPING DEMON PENIS"
- Pouya

Share this post


Link to post
Share on other sites
Try:


function dbAddDocument($name, $body, $username, $pass) {
if(!ValidateLogin($username, $pass) || !IsAdmin($username))
return -1;
$query = "INSERT INTO documents VALUES('','" . addslashes($name) . "','" . addslashes($body) . "')";
return mysql_query($query);
}


See if that helps in any way.

--hellz

[edited by - hellz on August 15, 2003 11:43:45 AM]

Share this post


Link to post
Share on other sites