PHP / SQL ile ilişkisel dizi Multi SQL Sorguları

0 Cevap php

Php / sql ile çoklu sorguları gönderme ve bir ilişkisel dizi içinde her sonuç eklemek hakkında çok basit bir soru var.

Temelde, bir ilişkisel dizi her sorgu atadık.

Each result for each queries go into my multi-dimensional array $el['nameofthequery']['resultsofthequery']

//connection information
 $host = "localhost";
 $user = "root";
 $password = "";
 $database = "sghi";
 $el = array();
 $query = array();

 //make connection
 $server = mysql_connect($host, $user, $password) or die('Could not connect to mysql server.');
 $connection = mysql_select_db($database, $server);

 //query the database
 $query['mylist1'] = mysql_query("SELECT CompanyName AS label,ContactName AS value FROM suppliers") or die('Could not select database.');
 $query['mylist2'] = mysql_query("SELECT ContactTitle AS label,City AS value FROM suppliers") or die('Could not select database.');

 //build array of results

  // Check if there is any results
  if(mysql_num_rows($query['mylist1']) == 0) {
   echo "No results found";
   exit; // so exit
  }
  else {
   while( $row = mysql_fetch_assoc( $query['mylist1']) ){
    $el['mylist1'][] = $row;
   }
  }

  // Check if there is any results
  if(mysql_num_rows($query['mylist2']) == 0) {
   echo "No results found";
   exit; // so exit
  }
  else {
   while( $row = mysql_fetch_assoc( $query['mylist2']) ){
    $el['mylist2'][] = $row;
   }
  }

 //echo JSON to page
 $response = json_encode($el);
 echo $response;
 mysql_close($server);

O kadar iyi çalışıyor, ama I would like to know if I'm using the best method in order to achieve that? oluyor

Here is the jquery script that handle the json data generated from my code above...it populate multiple list with different values in a form:

$.getJSON('test.php', function(result) {

    var optionsValues = '';
$.each(result['mylist1'], function(item) {
  optionsValues += '<option value="' + this.value + '">' + this.label + '</option>';
});
var options = $('#DestAirportList');
options.append(optionsValues);

    var optionsValues = '';
$.each(result['mylist2'], function(item) {
  optionsValues += '<option value="' + this.value + '">' + this.label + '</option>';
});
var options = $('#DestAirportRoomList');
options.append(optionsValues);

});

Cevabınız için teşekkürler!

0 Cevap