Home MySQL query not outputting result only heading
Reply: 2

MySQL query not outputting result only heading

BobK Published in 2017-12-08 00:03:17Z

I am currently practicing PHP and AJAX and I am trying to create an application that allows a user to search a database for product stock in real time. When the user types in a letter within one of the entries the headings show up but not the data. I'm not sure if I am doing it wrong but I'll let you guys be the judge of that. Code below:

Results.php - Below

 <!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="style.css">

$q = intval($_GET['q']);

$con = mysqli_connect("localhost", "username", "password", "databaseName");
//edited out for safety 

if (!$con) {
die('Could not connect: ' . mysqli_error($con));


$sql="SELECT * FROM stock WHERE name LIKE '%{$q}%'";
$result = mysqli_query($con,$sql);

echo "<table>
<th>Product name</th>

if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['descr'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "</tr>";

echo "</table>";

Nerds of Technology
Nerds of Technology Reply to 2017-12-08 00:13:48Z

$q = intval($_GET['q']); attempts to turn your input into an integer. Remove this line.

not_null Reply to 2017-12-08 08:50:59Z

you may try my code. I already tested this one. And everytime you entered a text, entered text will match some record from your database then if there's a record matched, then it will display the record matched on your entered text. Hope this helps :) Please note that the below code are the content of "ajax_search.php".

<!DOCTYPE html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
$(document).ready(function() {
$("#search").keyup(function() {
var name = $('#search').val();
if (name == "") {
else {
type: "POST",
url: "ajax_search.php",
data: {
search: name
success: function(html) {

<input type="text" id="search" placeholder="Search Product Here..." />
$con = mysqli_connect("localhost","username","password","databasename"); 
if (mysqli_connect_errno()) {
echo "Database Connection Failed: " . mysqli_connect_error();
if (isset($_POST['search'])) {
$key = $_POST['search'];
$query = "SELECT * FROM stock  WHERE name LIKE '%$key%' OR descr LIKE '%$key%' OR price LIKE '%$key%' OR quantity LIKE '%$key%' ";
$execute_query = mysqli_query($con, $query);
echo "<table border='3px' cellpadding='3px' cellspacing='3px' bgcolor='skyblue' align='center'>";
echo "<tr>";
echo "<td>Product Name</td>";
echo "<td>Description</td>";
echo "<td>Price</td>";
echo "<td>Quantity</td>";
echo "</tr>";
while ($result = mysqli_fetch_array($execute_query)) {
echo "<tr>";
echo "<td>".$result['name']."</td>";
echo "<td>".$result['descr']."</td>";
echo "<td>".$result['price']."</td>";
echo "<td>".$result['quantity']."</td>";
echo "</tr>";
<div id="display">
<!-- Ouput will display here -->
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303625 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO