4
このjsonのコンテンツをデータベースに挿入しようとしています。シングル引用符でデータベースに文字列を挿入する
しかし、テキストはそのようなものですので、私は概要でヌル結果を得る:
summary":"One of the oldest cities in the United States, Boston definitely has an \"old city\" feel to it. Home to great architecture and historical buildings, the city is also a great place to visit with younger ones, as it also plays host to LEGOLAND and many more children's attractions."
私は、これは、単一引用符と私のPHPのクエリ'{$summary}'
によるものだと思います。
$url = file_get_contents("urlcontent/api.json");
$arr = json_decode($url, true);
for($i=0;$i < count($arr);$i++){
$id = $arr[$i]['id'];
$location = $arr[$i]['location'];
$summary = $arr[$i]['summary'];
$query = "INSERT INTO [databasename].[dbo].[table](id , summary, location)";
$query .= "VALUES ('{$id}', '{$summary}', '{$location}')";
$update_query = sqlsrv_query($con, $query);
if(!$update_query){
die("There was an error" .print_r(sqlsrv_errors($con), true));
}
}
私はそれを修正しようとした:
$summary = preg_replace("'", "", $arr[$i]['summary']);
$summary = str_replace("'", "", $arr[$i]['summary']);
REPLACE('{$summary}','''','''') //MSSQL Database command
しかし、まだそのテキストを挿入し、NULL結果を得ることができません。私はecho
それとvar_dump
することができますし、テキストは私のテーブルに挿入することはできません。
私のデータベースにはtext
があります。
どのように動作させるのですか?歓声
私は私のIDは、あなたがあなたの問題を解決するために、あなたが今持っているSQLインジェクションの問題を回避するために準備されたステートメントを使用する必要があります – Maria
ような同じように働いているとの問題を持っていないです。詳細については、http://php.net/manual/en/function.sqlsrv-prepare.phpを参照してください。 – jeroen