का उपयोग कर एक MySQL डेटाबेस से कनेक्ट करना मैं एक एंड्रॉइड एप्लिकेशन विकसित करने वाली एक टीम में हूं जो रिमोट डेटाबेस के उपयोग पर काफी निर्भर करेगा। हम फोनगैप और jQuery मोबाइल का उपयोग कर रहे हैं और AJAX और JSON कॉल का उपयोग करके हमारे MySQL डेटाबेस से कनेक्ट करने का प्रयास कर रहे हैं। वर्तमान में, हमें हमारे परीक्षण चरण में परेशानी हो रही है, जो यह सत्यापित करना है कि हमारे MySQL/इनपुट से MySQL Workbench के माध्यम से "टेड" के हार्ड-कोड किए गए उपयोगकर्ता को खींचकर भी एक कनेक्शन है।फोनगैप, AJAX, और JQuery मोबाइल
हम क्या इकट्ठे हुए हैं से, आंकड़ा संचरण की प्रक्रिया के रूप में काम करता है:
हमारे html फ़ाइल पर, हम एक
<script type="text/javascript" src="Connect.js"></script>
^कौन सा Connect.js स्क्रिप्ट चलाना चाहिए , सही बात? तो वहां से, Connect.जेएस भाग गया है?
कनेक्ट.जेएस रन करता है, जो इसे हमारे सर्वरफाइल.एफ़पी से कनेक्ट करता है जो बाहरी वेब सेवा पर होस्ट किया जाता है, जिससे यह 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 डेटाबेस से कनेक्ट होना चाहिए, चयन कथन बनाएं, और फिर 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));
}
?>
फिर भी कुछ भी नहीं दिखा रहा है। हम यहाँ से क्या करते हैं?
क्या आपने समस्या को कोड में कहां पहचानने के लिए कोई डिबगिंग किया है? मैं 'echo json_encode (सरणी (' स्थिति '=>' सफलता ') डालने की अनुशंसा करता हूं); बाहर निकलें; 'ServerFile.php के उद्घाटन पर यह देखने के लिए कि क्या समस्या यह है कि यह एसक्यूएल के साथ कहीं भी दबाया जा रहा है, या अगर यह वापस आने वाली समस्या को पढ़ने में समस्या है। –
या, डीबग करने का एक और तरीका 'console.log (प्रतिक्रिया) रखना होगा,' बस अपने $ .getJSON फ़ंक्शन के अंदर। प्रतिक्रिया तब आपके फायरबग में दिखाई देगी। –