DragonPrime - LoGD Resource Community
Welcome Guest
  • Good afternoon, Guest.
    Please log in, or register.
  • October 22, 2017, 12:19:12 PM
Home Forums News Downloads Login Register Advanced Search
* * *
DragonPrime Menu
Login
 
 
Resource Pages
Search

Pages: [1]   Go Down
  Print  
Author Topic: sql and '  (Read 2225 times)
0 Members and 1 Guest are viewing this topic.
HADES-HL
Guest
« on: October 14, 2004, 01:34:23 PM »

How can I save strings containing ' in a MySQL database?

I want to save a $badguy array and $badguy['creaturelose'] contains sometimes a '

And I always get an error, could someone help me?
Logged
Excalibur
Member
Mod God
*****
Offline Offline

Posts: 573


I'm a newbie, plz forgive me!


View Profile WWW
« Reply #1 on: October 14, 2004, 02:52:06 PM »

Well, the problem is that "'" is a reserved char by MySQL. So you need to add "\" before it, to let MySQL know that you want to save ' like a char in the string (like "`\$Hello" to get Hello in LoGD instead of "`$Hello" that will get you unwanted output)

So you need a simple script to convert all "'" in "\'"
Code:
$dontwork = "'";
$work = "\'";
$newbadguyname = str_replace($dontwork, $work, $badguyname);
Be sure to save $newbadguyname instead of $badguyname in your SQL command and have a look for more info on str_replace command here.
« Last Edit: October 14, 2004, 02:56:44 PM by Excalibur » Logged

Spock: Random chance seems to have operated in our favor.
McCoy: In plain, non-Vulcan English, we've been lucky.
Spock: I believe I said that, Doctor.
Tholgare
Guest
« Reply #2 on: October 14, 2004, 03:02:11 PM »

you can also do:
Code:
$fixedstring = addslashes(trim(stripslashes($badstring)));

Then don't forget to remove the slashes whne getting data later:
Code:
$original = trim(stripslashes($fixedstring_from_db));

The trim just gets rid of any unneeded whitespace.   If you want to be really thorough, do this on all your arrays:
Code:
reset($array);
while(list($k,$v)=each($array))
    $array[$k]=addslashes(trim(stripslashes($v)));

and then do the same without the addslashes part in order to clean up values before display.  That'll take care of all the various special chars, not just  '
Logged
Kendaer
Guest
« Reply #3 on: October 14, 2004, 11:36:55 PM »

You can also use  safeescape() in lib/safeescape.php which operates slightly differently than addslashes() (which can escape more than is needed) and is basically a pre-existing function which does the str_replace() that someone else suggested.

Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


*
DragonPrime Notices
Please take the time to read the FAQ and browse the DragonPedia

Support Us
No funds raised yet this year
Your help is greatly appreciated!
Recent Topics
DragonPrime LoGD
Who's Online
27 Guests, 0 Users
Home Forums News Downloads Login Register Advanced Search