[SOLVED] String problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

[SOLVED] String problem

Post by S_henry »

Let say i have 2 string variables:

Code: Select all

$a = "Book of student";
$b = "Student's book";

$query1 = "INSERT INTO table1 (str1) VALUES ('$a')";
...

$query2 = "INSERT INTO table1 (str1) VALUES ('$b')";
...
If i execute query1 and query2, query1 is successed and query2 is failed. This is because in string $b, there is a character ('). So anybody knows how to solve this problem? I mean the problem if we want to save a string variable that contain a character (').
Last edited by S_henry on Wed May 18, 2005 2:22 am, edited 1 time in total.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

addslashes()

Code: Select all

$b = addslashes("Student's Book");
$query2 = "INSERT INTO table1 (str1) VALUES ('$b')";
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

use mysql_escape_string() to escape ALL the chars that might cause it to bomb...
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

Yes. Perfect! Thanx guys.. :D
Post Reply