Search code examples
phpmysqlescaping

How do I store and retrieve a colon (:) in a MySQL field?


I'm simply trying to store a URI in a MySQL table. When I retrieve it the ":" disappears. So http://www.example.com becomes http//www.example.com I've researched it and there are solutions for ; (semicolons), , (commas), quote marks et etc., but not for colons.

I've tried http \ ://www.xyz.com (no spaces) but that converts it http/://www.example.com I'm using php. Any suggestions?

Here is the code:

<?php
$id = $_GET["id"];
$conn = new mysqli($servername, $username, $password, $dbname); //security parameters not shown

$sql = "SELECT * FROM uri WHERE myURI='" . $id . "'";
$result = $conn->query($sql);
if ($result) {
    $row = $result->fetch_assoc();
    if ($row) {
        $SendTo = $row["SendTo"];
        header("Location: " . $SendTo);
    }
}
?>

Note: I am not showing insertion code because at this stage I am simply entering the URI manually to create a POC.


Solution

  • Use mysqli_real_escape_string() function to escape slashes and store safely in database

    For e.g.

    $url  = "http://www.example.com";
    $safeURL = mysqli_real_escape_string($url);
    

    and then use $safeURL var in SQL to store.