2017-08-22 7 views
0

私はデータベースを照会してjsの機能に結果を得るためにjavascriptからphpメソッドを呼び出そうとしています。現在、私のAJAXである'console.log(output)は、ただ出力している:なぜそれがこれをやっているAJAXが正しいものを出力しない

"array (size=1) 
'action' => string 'getResults' (length=10)'" 

本当にわからない、それはデータベースからただ一つのエントリであるクエリ結果を返すべきです。誰もが考えている?どんな助けも歓迎です!ありがとう。私のJavascriptの

パートがファイル:

function callPHP() { 
    $.ajax ({ 

     type: "GET", 
     datatype: "application/json", 
     url: "BaseClass.php", 
     data: { action : 'getResults' }, 
     //error: function(err){console.log(err)}, 
     success: function(output) { 

      console.log(output); 
      //alert(output); 
     } 
     //error, function(err){console.log(err)} 
    }); 


} 

callPHP(); 

をBaseClass.php:

<?php 

    error_reporting(E_ALL); ini_set('display_errors', 1); 

    require("Conn.php"); 
    require("MySQLDao.php"); 

    $param=$_REQUEST['action']; 

    echo var_dump($_GET); 
    /* 
    $handle = fopen("php://input", "rb"); 
    $param = ''; 
    while (!feof($handle)) { 
     $param .= fread($handle, 8192); 
    } 
    fclose($handle); 
    */ 

    if (empty($param)) 
    { 
     $returnValue["status"] = false; 
     $returnValue["title"] = "Error"; 
     $returnValue["message"] = "No Data Recieved paige" .$param ."..."; 
     echo json_encode($returnValue); 
     return; 
    } 
    else 
    { 
     $dao = new MySQLDao(); 
     if ($dao->openConnection() == false) 
     { 
      $returnValue["status"] = false; 
      $returnValue["title"] = "Error"; 
      $returnValue["message"] = "Connection Could Not Be Established Between Server And Database"; 
      echo json_encode($returnValue); 
     } 
     else 
     { 
      //Decodes data, dont change 
      $body = json_decode($param, true); 
      $recieved = $body["data"]; 

      //Gets the result of a query 
      //$result = $dao->MySQLDaoMethodName(parameters); 

      //Return the result of the query 
      //echo json_encode($result); 
     } 
     $dao->closeConnection(); 
     return; 
    } 
?> 

Conn.php - これは、すべての接続情報である、*アウト機密の理由のために: *

<?php 

    error_reporting(E_ALL); ini_set('display_errors', 1); 

    class Conn 
    { 
     public static $dbhost = "***"; 
     public static $dbname = "***"; 
     public static $dbuser = "***"; 
     public static $dbpass = "***"; 
    } 
?> 

MySQLDao.php - このファイルはquerysを保持している:

<?php 

    error_reporting(E_ALL); ini_set('display_errors', 1); 

    //Class for holding queries 
    class MySQLDao 
    { 
     var $dbhost = null; 
     var $dbuser = null; 
     var $dbpass = null; 
     var $mysqli = null; 
     var $dbname = null; 
     var $result = null; 


     //constructor 
     function __construct() 
     { 
      $this->dbhost = Conn::$dbhost; 
      $this->dbuser = Conn::$dbuser; 
      $this->dbpass = Conn::$dbpass; 
      $this->dbname = Conn::$dbname; 
     } 

     //Attempt a connection to the database 
     public function openConnection() 
     { 

      //Try and connect to the database 
      $this->mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); 
      //If the connection threw an error, report it 
      if (mysqli_connect_errno()) 
      { 
       return false; 
      } 
      else 
      { 
       return true; 
      } 
     } 

     //Get method for retrieving the database conection 
     public function getConnection() 
     { 
      return $this->mysqli; 
     } 

     //Close the connection to the database 
     public function closeConnection() 
     { 
      //If there is a connection to the database then close it 
      if ($this->mysqli != null) 
       $this->mysqli->close(); 
     } 

     //-----------------------------------QUERY METHODS------------------------------------- 

     public function getResults($data) 
     { 

      $sql = "SELECT room.room_description FROM room WHERE room.room_id = 1"; 

      $result = $this->mysqli->query($sql); 


      //if (mysql_num_rows($result) == 1) { 
      // $obj = mysql_fetch_object($result, 'obResults'); 

      //} 

      echo json_encode($result); 

      echo($result); 

     } 

    } 
?> 
+0

変換 'タイプ:、 "POST"'とだけ –

+0

を確認します。type ''に、 "GET" '

 array(size=0) empty 
' – Paige

+0

これを変更すると、 '$ _POST'や' $ _REQUEST'もチェックする必要があります。 – ficuscr

答えて

0

私はあなたがjavscriptとPHPの間でデータを出荷する方法を誤解だと思います。

  1. オブジェクトまたはデータの配列を送信する場合は、javascriptで$ .post()を指定してください。
  2. あなたのPHPはjavascriptからjsonを受け取ります。 phpのjson_decode関数を使用してPHPに役立てる必要があります。
  3. phpスクリプトの出力をjavascriptに役立たせたい場合は、phpのjson_encode関数でエンコードしてから、それを呼び出しスクリプトに戻す必要があります。

http://php.net/manual/en/function.json-decode.php

+0

しかし、私はPHPからjavascriptに送信したいので、jsがデータを取得できるようにphpで投稿しなければならないと思った – Paige

+0

ただ私の答えを更新しました。それを行う方法の詳細については、手順3を参照してください。 – catbadger

0

出力はecho var_dump($_GET);のために、私は確信しています。私は、出力形式がvar_dumpタイプであるために分かります。コードの成功部分には出力がありません。私はこの部分でelse { //Decodes data, dont change ...の出力がコメントアウトされていることを意味します。

私はmysqliのを使用しているが、いくつかの方法が、私は多くの不完全な方法や機能を見ることができるように、このコードは、完全とデバッグ段階ではないと仮定したコード

//if (mysql_num_rows($result) == 1) { 
     // $obj = mysql_fetch_object($result, 'obResults'); 

//} 

のこの部分のようにMySQLのAPIにあることに気づきましたコール。

また、セキュリティのためにプレースホルダを備えた準備文を使用するようにしてください。

API出力の前にob_clean()を使用することをお勧めします。余分な文字が出力データとフォーマットを破壊するためです。ただし、エラーは表示されません。ブラウザレストクライアント拡張のようなAPIテストに便利なツールがあります。デバッグの最善の方法は、常にデバッグツールやx-debugなどのフレームワークです。

コードを次のように変更してください。お役に立てれば!

BaseClass.php:

<?php 

    error_reporting(E_ALL); ini_set('display_errors', 1); 

    require("Conn.php"); 
    require("MySQLDao.php"); 

    $param=$_REQUEST['action']; 

    // echo var_dump($_GET); 
    /* 
    $handle = fopen("php://input", "rb"); 
    $param = ''; 
    while (!feof($handle)) { 
     $param .= fread($handle, 8192); 
    } 
    fclose($handle); 
    */ 

    if (empty($param)) 
    { 
     $returnValue["status"] = false; 
     $returnValue["title"] = "Error"; 
     $returnValue["message"] = "No Data Recieved paige" .$param ."..."; 
     ob_clean(); 
     echo json_encode($returnValue); 
     exit(); 
    } 
    else 
    { 
     $dao = new MySQLDao(); 
     if ($dao->openConnection() == false) 
     { 
      $returnValue["status"] = false; 
      $returnValue["title"] = "Error"; 
      $returnValue["message"] = "Connection Could Not Be Established Between Server And Database"; 

      //Clean up before output 
      ob_clean(); 
      echo json_encode($returnValue); 
      exit(); 
     } 
     else 
     { 
      //Decodes data, dont change 
      $body = json_decode($param, true); 
      $recieved = $body["data"]; 

      //Gets the result of a query 
      $result = $dao->getResults($recieved); 
      //Close connection as fast as possible 
      $dao->closeConnection(); 

      //Return the result of the query 
      ob_clean(); 
      echo json_encode($result); 
      exit(); 
     } 
    } 
?> 

MySQLDao.php

<?php 

    error_reporting(E_ALL); ini_set('display_errors', 1); 

    //Class for holding queries 
    class MySQLDao 
    { 
     var $dbhost = null; 
     var $dbuser = null; 
     var $dbpass = null; 
     var $mysqli = null; 
     var $dbname = null; 
     var $result = null; 


     //constructor 
     function __construct() 
     { 
      $this->dbhost = Conn::$dbhost; 
      $this->dbuser = Conn::$dbuser; 
      $this->dbpass = Conn::$dbpass; 
      $this->dbname = Conn::$dbname; 
     } 

     //Attempt a connection to the database 
     public function openConnection() 
     { 

      //Try and connect to the database 
      $this->mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); 
      //If the connection threw an error, report it 
      if (mysqli_connect_errno()) 
      { 
       return false; 
      } 
      else 
      { 
       return true; 
      } 
     } 

     //Get method for retrieving the database conection 
     public function getConnection() 
     { 
      return $this->mysqli; 
     } 

     //Close the connection to the database 
     public function closeConnection() 
     { 
      //If there is a connection to the database then close it 
      if ($this->mysqli != null) 
       $this->mysqli->close(); 
     } 

     //-----------------------------------QUERY METHODS------------------------------------- 

     public function getResults($data) 
     { 

      $sql = "SELECT room.room_description FROM room WHERE room.room_id = ?"; 

      $stsm = $this->mysqli->prepare($sql); 
      $stsm->bind_param('i',1); 
      $result = $stmt->execute(); 

      if (mysqli_num_rows($result) == 1) { 
       $obj = mysqli_fetch_object($result, 'obResults'); 
       return $obj; 
      } 

      return false; 
     } 

    } 
?> 
関連する問題