- Forums
- PHP
- How To Insert & & Ampersand In Database
Im Having Some Problems Entering The Ampersand Character In Mysql If You Have The Same Problem We Will Show You Some Tips [2071], Last Updated: Mon Jun 24, 2024
Webune Support
Thu Dec 10, 2009
1 Comments
2281 Visits
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%26tyou 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:
"I HAVE AT&T EMAIL ACCOUNT"
https://www.webune.com/forums/how-to-insert-amp-ampersand-in-database.html