2013-03-29 8 views
6

私はリモートデータベースの使用に大きく依存するAndroidアプリケーションを開発しています。私たちはPhoneGapとJquery Mobileを使用しており、AJAXとJSON呼び出しを使用してMySQLデータベースに接続しようとしています。現在、私たちはテストフェーズで問題を抱えています。これは、MySQL Workbenchを介してmyql/inputから "Ted"のハードコードされたユーザーを引き抜くことによって、接続することさえ確認します。私たちが集まってきたものからPhoneGap、AJAX、およびJQuery Mobileを使用してMySQLデータベースに接続する

、データ伝送のプロセスがこのように動作します。私たちのhtmlファイルで

  1. 、我々はConnect.jsスクリプトを実行する必要があります

    <script type="text/javascript" src="Connect.js"></script> 
    

    ^を持っています、正しい?そこから、Connect.jsが走っていますか?

  2. Connect.jsが実行され、外部WebサービスでホストされているServerFile.phpに接続され、PHPを実行してMySQLデータベースに接続して情報を取得できます。

    //run the following code whenever a new pseudo-page is created 
    $('#PAGENAME').live('pageshow', function(event)) { 
    
        // cache this page for later use (inside the AJAX function) 
        var $this = $(this); 
    
        // make an AJAX call to your PHP script 
        $.getJSON('http://www.WEBSITENAME.com/ServerFile.php', function (response) { 
    
         // create a variable to hold the parsed output from the server 
         var output = []; 
    
         // if the PHP script returned a success 
         if (response.status == 'success') { 
    
          // iterate through the response rows 
          for (var key in response.items) { 
    
    
           // add each response row to the output variable 
           output.push('<li>' + response.items[key] + '</li>'); 
          } 
    
         // if the PHP script returned an error 
         } else { 
    
          // output an error message 
          output.push('<li>No Data Found</li>'); 
         } 
    
         // append the output to the `data-role="content"` div on this page as a 
         // listview and trigger the `create` event on its parent to style the 
         // listview 
         $this.children('[data-role="content"]').append('<ul data-role="listview">' + output.join('') + '</ul>').trigger('create'); 
        }); 
    }); 
    

ここServerFile.phpです。これは、MySQLデータベースに接続し、Select文を作成し、JSON形式でエンコードされたブラウザに出力を送信する必要があります。

<?php 

//session_start(); 
$connection = mysql_connect("csmadison.dhcp.bsu.edu", "clbavender", "changeme"); 
$db = mysql_select_db("cs397_clbavender", $connection); 

//include your database connection code 
// include_once('database-connection.php'); 

//query your MySQL server for whatever information you want 
$query = mysql_query("SELECT * FROM Users WHERE Username ='Ted'", $db) or trigger_error(mysql_error()); 

//create an output array 
$output = array(); 

//if the MySQL query returned any results 
if (mysql_affected_rows() > 0) { 


    //iterate through the results of your query 
    while ($row = mysql_fetch_assoc($query)) { 

     //add the results of your query to the output variable 
     $output[] = $row; 
    } 


    //send your output to the browser encoded in the JSON format 
    echo json_encode(array('status' => 'success', 'items' => $output)); 

} else { 

    //if no records were found in the database then output an error message encoded in the JSON format 
    echo json_encode(array('status' => 'error', 'items' => $output)); 
} 
?> 

ここには何も表示されていません。私たちはここから何をしていますか?

+0

コードのどこに問題があるのか​​を特定するためにデバッグを行いましたか? 'echo json_encode(array( 'status' => 'success'));を置くことをお勧めします。 exit; ServerFile.phpのオープン時に問題がSQLのどこかで詰まっているのか、何が戻ってくるのかという問題があるかどうかを確認してください。 –

+1

また、デバッグするもう一つの方法は$ .getJSON関数の中に 'console.log(response);'を置くことです。応答はFirebugに表示されます。 –

答えて

2

最初のものが最初です。サーバー側またはクライアント側のどこから問題が発生しているのかを判断してください。

データベースクエリとエンコードされたjsonを印刷すると便利です。シンプルなAPIサービスを作成する場合は、ブラウザを使用してhttp://www.WEBSITENAME.com/ServerFile.phpと入力して、出力がどのようになっているかを調べる必要があります。

phpで物を印刷するにはechoを使用します。

すべてが正常であれば、サーバーから受信した応答をプリントアウトして、JavaScriptがオフになっているかどうかを確認してください。

javascriptで物を印刷するにはconsole.logを使用してください。ログはeclipseのlogcatセクションに表示されます(あなたはAndroidアプリを開発しているので)

関連する問題