How To Insert & & Ampersand In Database

RegisterLogin
How To Insert & & Ampersand In Database
Post Description: Im Having Some Problems Entering The Ampersand Character In Mysql If You Have The Same Problem We Will Show You Some Tips
Tags: How, To, Insert, &, Ampersand, In, Database
This Post Was Posted On Dec 10, 2009 By Webune Support #2881
Welcome to Webune Support forums

if you like programming in php and you have problems entering your data or a string into your MySQL database when the strin contains the ampersand charater: &

we will show you some times what you can use to be sure that this character is entered connected.

lets say for example, i have this PHP code:

<?php
$string = "AT&T Email"
$Sql = "INSERT INTO mytable SET keyword='$string'";
?>


if you were to try to enter this code into your database, you probably would not get any error

but how about if you are getting the value of the string from a URL, like this for example:

the url is: http://www.example.com/example.php?string=at&t
<?php
$string = $_GET['string'];
$Sql = "INSERT INTO mytable SET keyword='$string'";
?>


the only characters that would be inserted into your dabase will be "AT"
any character after & will not be entered.

so how do you solve this.

you will have to correct the url to look like this:
http://www.example.com/example.php?string=at%26t

you see what i did? i change & to %26

%26 is the the encoded code for &

to encode url, PHP provides the urlencode() function. to decode it you would simply use the urldecode() function

hope that helps

IMPORTANT: this tutorial only applies in situations where you are getting the string from the url,

it its a regular string embed into HTML, then you would have to use htmlspecialchars() function for example

<?php
$string = htmlspecialchars("I HAVE AT&T EMAIL ACCOUNT");
echo $string;
?>

in the above example, the value of $string would be the HTML code like this:
&quot;I HAVE AT&amp;T EMAIL ACCOUNT&quot;
Leave Your Comments
Related Pages: [Add Your Website]
Post New Topic
guly
#3285 1
can you update string with ampersand in database?
Jan 20, 2010 Reply Report abuse
©2011 Webune Forums - Mon Dec 19, 2011 7:14 am
Powered by: Webune Forums V3