Junaid Amjad Junaid Amjad - 6 months ago 9
SQL Question

My select query is not working in ajax

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"

Answer

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'];
}
Comments