0

here is the code which i use to fetch all medicines name from database in dropdown list

  <?php 
    $selmed = mysql_query("SELECT mnam FROM med");
    echo '<select onChange="getQty();" id="pf5" name="recmed">';
    while ($row = mysql_fetch_array($selmed)) 
    {echo '<option value="'.$row['mnam'].'">'.$row['mnam'].'</option>';} 
  ?>

Now I want to fetch quantity against a specific medicine from database for that i use ajax as follow

var medn = $('#pf5').val();
$.ajax({
  type: "POST",
  url: "getqty.php",
  data: {
    mednam: medn
  },
  success: function(data) {
    $("#val").html(data);
  }
});
}

and here is my getqty.php file where i think i am making some mistake in query

<?php
include('connection.php');
$recm = $_POST['mednam'];
$rmq = mysql_query("SELECT mqty FROM med WHERE mnam ='$recm'");
echo $rmq;
?>

and the area where i want result on changing value shows "Resource id #5"

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 09 '16 at 11:18
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) – RiggsFolly May 09 '16 at 11:18
  • what is the output you are getting now? – Madhawa Priyashantha May 09 '16 at 11:19
  • Your issue is you are not processing the result of the `mysql_query()` All that does is execute the query on the server. You have to then fetch the rows that are returned `mysql_fetch_assoc()` for example – RiggsFolly May 09 '16 at 11:19
  • my output is **Resource id #5** – Junaid Amjad May 09 '16 at 11:34
  • you don't do select queries in ajax. – e4c5 May 09 '16 at 11:41

2 Answers2

0

The following line does not echo result data

$rmq = mysql_query("SELECT mqty FROM med WHERE mnam ='$recm'");
echo $rmq;

Use while loop to echo all results

$rmq = mysql_query("SELECT mqty FROM med WHERE mnam ='$recm'");
while ($row = mysql_fetch_assoc($rmq)) {
    // echo fields with
    // $row['field_name'];
}
Deepak Adhikari
  • 419
  • 2
  • 4
0

You've made the query, but not $rmq can not be interpreted as the data you want until you process it

$rmq = mysql_query("SELECT mqty FROM med WHERE mnam ='$recm'");
while ($row = mysql_fetch_array($rmq)) { //Not necessarily a while-loop, depends on what data you're expecting
    $var = $row['x'];
    echo $var;
}

Please consider using PDO, or at the very least MySQLi

Algernop K.
  • 477
  • 2
  • 19