无法在MySql数据库中存储Base64数据

时间:2014-06-27 05:56:57

标签: php mysql string base64

我正在尝试使用PHP将Base64字符串存储到MySql数据库中。这是我的代码。

<?PHP
//Store Image
$photoData = $_POST['PhotoData'];

//Database connection details
$db_url = "192.168.1.140";
$db_user_name = "easyblogadmin";
$db_password = "admin";
$db_name = "easyblog";

$con = mysqli_connect($db_url, $db_user_name, $db_password);
$sql = "insert into temptable (photodata) values('$photoData')";

if(mysqli_connect_errno()){
        echo "Failed to Connect Database";
        echo mysqli_connect_errno();
        die();
    } else {
        //Connecting to Application Database
        $db_selected = mysqli_select_db($con, $db_name);
        if(!$db_selected){
            echo "Failed to Connect Application Database";
            die();
        } else {
            if (!mysqli_query($con,$sql)) {
                die('Error: ' . mysqli_error($con));
            }
            mysqli_close($con);
            echo "Successfully Added photo".strlen($photoData);
        }
    }?>

这里$ photoData是我的Base64编码字符串,我将其存储在我的数据库中。当我尝试从DB检索此数据时,我的字符串中的某些数据丢失了。我不明白为什么会这样。谁能帮我吗。

问候。

1 个答案:

答案 0 :(得分:0)

更改sql查询,如下所述

$sql = "insert into databasename.temptable (photodata) values('$photoData')";

还请确保编码值是否有任何单引号,如果是,则替换下面提到的数据。

$photoData =str_replace("'","\'",$_POST['PhotoData']);