简体   繁体   中英

Fetch data in the format [1,2,3] from mysql database using php and jquery

I have my jquery code which connects to getdata.php . Here the val is dynamic value and the function is called every time an option is selected from dropdown.

    function getMoleculeData(val){
      var molval=val;
      var url = 'getmoldata.php';
     $.ajax({ 
      url: url,
      data: 'molval='+molval,
      method:'POST',
      success: function(moldata) {        
      alert(moldata);
        }
     });
    }

My getdata.php code goes here

    <?php  
      $molval = $_POST['molval']; //  consider $_POST['molval']=ABC 
      $dbhost = "localhost";
      $dbuser = "root";
      $dbpass = "";
      $dbname= "userdata";
      $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      mysql_select_db($dbname) or die ();   
       $query = "SELECT cm_id, CORP_RANK, CORP_MKT_SHARE, COMBINED_MOLECULE FROM PRODUCT_UNIVERSE_D WHERE COMBINED_MOLECULE ="."'$molval'";

       $result = mysql_query($query) or die(mysql_error());
       $i = 1; 
         $array = array();
         while($row = mysql_fetch_array($result) { 
         $array[] = $row;
         echo $row['CORP_RANK']; // etc
         } 

       ?>

Here is my table PRODUCT_UNIVERSE_D

   cmid  |  CORP_RANK | CORP_MKT_SHARE |  COMBINED_MOLECULE  |    DATE
     1   |     10     |       30       |         ABC         |  01-04-2013
     2   |     5      |       50       |         ABC         |  03-06-2013
     3   |     8      |       40       |         ABC         |  23-09-2013
     4   |     3      |       10       |         XYZ         |  05-01-2014

when I select ABC in dropdown, the same value is given in where clause and I am able to fetch the result. FOr the above code written in getdata.php the output that I got is 1058 ie the values of CORP_RANK where COMBINED_MOLECULE = 'ABC'. But the requirement is I have to get the result in the form of [10,5,8] . How do I get the result in this array format and also how to fetch all the columns separately like $row['CORP_MKT_SHARE'] = [10,50,40] and display them separately in success function of index.php as

    [10,5,8] and [10,50,40] for furthur use

Please help me out, as I am not perfect in fundamentals of arrays I am unable to get the result. Thanks in advance

If you're aiming to output the data into json format.

example:
$array = array(1,2,3);
var_dump(json_encode($array));

You'll get this output:

string(7) "[1,2,3]" 

It's better to change your loop as this:

for($array = array();$row = mysql_fetch_array($result);$array[] = $row);
json_encode($array);

This is what the best way when you want to retrieve data from php file using ajax.

using your code:

while($row = mysql_fetch_array($result)) { 
   $rank[] = $row['CORP_RANK'];
   $share[] = $row['CORP_MKT_SHARE'];
}
$array = array('CORP_RANK'=>$rank,'CORP_MKT_SHARE'=>$share);
json_encode($array);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM