2017-11-03 10 views
0

I以下のAjaxリクエストがありますPHP/jQueryのAjaxのリターンSQLiteの文字列

getContent.php

getContent.phpは非常に簡単ですで私のPHPスクリプトに単一の値を送信している

$.ajax({ 
      type: "POST", 
      url: "getContent.php", 
      dataType: "text", 
      data: { "contentid": contentID }, 
      success: function (data) { 
       console.log("Received: " + data); 
      } 
     }); 
    } 

、おもう。

<?php 
// Set up connection to sqlite 
try { 
    $myPDO = new PDO('sqlite:freezy.sqlite'); 
} catch (PDOException $e) { 
    echo "Failed to get DB handle: " . $e->getMessage() . "\n"; 
    exit; 
} 

$id = $_POST['contentid']; 

// Prepare an update statement 
$sql = "select contentBlock from content where contentId = :contentIDTarget"; 

// formalise the statement 
$stmt = $myPDO->prepare($sql); 

// passing values to the parameters 
$stmt->bindValue(':contentIDTarget', $id); 

// Execute the update 
$result = $stmt->execute(); 

echo $result; 

?> 

このデータベースは、同じデータベースに対してslqiteテストツールでテストしたときに正常に動作します。

私はしかし、これを使用しようと、私は 「受信:」上のコンソールログに表示さのすべての行がある:受信

:私は見て期待していた何1

のようなものでした。 受信:

テストテキスト

結果には、tinyMCEからそこに配置されたコンテンツが含まれている必要があるためです。

私には何が欠けていますか?操縦者に大いに感謝します。

答えて

2

あなたの声明...

$result = $stmt->execute(); 

はブール値を返します。だから、 '1'はそれがうまくいくという意味では良いことです。

実際の結果を返す必要があります。 あなたが何かのような...

$result = $sth->fetch(PDO::FETCH_ASSOC); 
何かのように変換さ

...

// Execute the update 

$stmt->execute(); 
$result = $sth->fetch(PDO::FETCH_ASSOC); 
echo $result; 

またはecho $result['contentBlock'];

を試みることができるそれはすべてあなたがそれを使用する方法によって異なります。

これは役に立ちますか?

+1

ありがとう@TimBrownlaw - 大変感謝しています! 私は今使用しています: //更新を実行 $ stmt-> execute(); $結果= $ sth->フェッチ(PDO :: FETCH_ASSOC); echo $ result ['contentBlock']; とAjaxリクエストで私が使用しています: $アヤックス({ タイプ: "POST"、 URL: "getContent.php"、 データ:{ "コンテンツID":コンテンツID}、 成功:機能(データ){ console.log( "受信:" +データ); } }); } しかし、私は500のサーバーエラーが発生しています。まだチェック中... – user3202399

関連する問題