ajax - Display database value in array PHP MYSQLi -
i"m attempting display data i've sent ajax php file, reason not displaying on page. way works enter search term input field, , ajax script post value php script, return database value requested back.
error_reporting(e_all); ini_set('display_errors', '1'); if (isset($_post['name']) === true && empty($_post['name']) === false) { //require '../db/connect.php'; $con = mysqli_connect("localhost","root","root","retail_management_db"); $name = mysqli_real_escape_string($con,trim($_post['name'])); $query = "select `names`.`location` `names` where`names`.`name` = {$name}"; $result = mysqli_query($con, $query); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result)) { $loc = $row['location']; echo $loc; }//close while loop } else { echo $name . "name not found"; } }
html form:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>retail management application</title> </head> <body> name: <input type="text" id="name"> <input type="submit" id="name-submit" value="grab"> <div id="name-data"></div> <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script> <script src="js/global.js"></script> </body> </html>
you're appending mysql error result query, , you're trying query query result, try following:
$query = "select `names`.`location` `names` where`names`.`name` = '$name'"; $result = mysqli_query($con, $query); if (mysqli_num_rows($result) > 0) {
edit:
{$name}
string , should quoted instead.
change '$name'
in where
clause.
using:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
will provide reason why query failed.