php - ajax response returns [object] using pdo and show my query -
getting querystring sent ajax response. ajax works know id being passed , have echo variable in php , show correct when use json encode [object] alert in , response looks
response:
{"querystring":"select * contactinfo id = :id"} php:
$id = $_post['id']; if (empty($id)) { echo "no id"; } try { $conn = new pdo('mysql:host=localhost;dbname=test;charset=utf8', user, pass); $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $conn->prepare('select * contactinfo id = :id'); $stmt->execute(array('id' => $id)); echo json_encode( $stmt); } catch(pdoexception $e) { echo 'error: ' . $e->getmessage(); } js:
$.ajax({ type: "post", timeout: 6000, data : {id: myid}, // add if using post datatype : 'json', //text crossdomain: false, cache: false, async: true, url: requrl, success: function(data) { alert(data); }
you need pass result array use fetch():
$result = $stmt->fetch(pdo::fetch_assoc); echo json_encode( $result ); then jquery return object here's how can access it:
success: function(data) { alert(data.firstname); } edit: noticed you're returning 1 row it's not fetchall().
Comments
Post a Comment