Displaying MySQL in PHP - php

So far, I have this program that links my phpmyadmin database to my php script. Now, I need to display certain things in a table like "all records," "all contacts whose last name starts with S," "all pet owners," etc. My question is: is there a simpler way to insert code into my php script to display the information from my database. Right now I have that long echo statement to display the information. Is there a way I can just use something like a SELECT * statement to display all records and to simplify my code?
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli( $db_hostname,
$db_username,
$db_password,
$db_database);
if ($connection->connect_error) {
echo "Sorry";
} else {
echo "Connected!<br><br>";
$sql = "SELECT * FROM People";
$result = $connection->query($sql);
if (!$result) die ($connection->error);
$n = $result->num_rows;
for ($i=1; $i<=$n; $i++) {
$row = $result->fetch_array(MYSQLI_ASSOC);
echo "<table>
<tr><th>ID</th><th>First Name</th><th>Last Name</th>
<th>Street Address</th><th>City</th>
<th>State</th><th>Zip Code</th>
<th>Email Address</th><th>Comment</th>
<th>Number of pets</th></tr>";
echo "<tr><td width=20>" . $row['iD'] . "</td><td>" . $row['First Name'] . "</td><td width=40>" .
$row['Last Name'] . "</td><td width=200>" . $row['Street Address'] . "</td><td width=30>" .
$row['City'] . "</td><td width=40>" . $row['State'] . "</td><td width=30>" .
$row['Zip Code'] . "</td><td width=40>" . $row['Email Address'] . "</td><td width=20>" .
$row['Comment'] . "</td><td width=10>" . $row['Number of pets'] . "</td></tr>";
}
echo "</table>";
}
?>

You should a table structure first then insert your PHP codes within the structure. E.g:
<?php
// Put your MYSQLI retrievals codes here
?>
<table>
<tr>
<th>FIELD_1</th>
<th>FIELD_2</th>
<th>FIELD_3</th>
</tr>
<?php
while ($rows = $result->fetch_array(MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $rows['field_1']; ?></td>
<td><?php echo $rows['field_2']; ?></td>
<td><?php echo $rows['field_3']; ?></td>
</tr>
<?php
}
?>
</table>

Technically you are doing everything alright, but there are some more sophisticated ways to develop the things you want.
Take a look at the following links:Object oriented programming in PHP and templating in PHP.

Hope this works for you:
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli($db_hostname,$db_username,$db_password,$db_database);
if ($connection->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
echo "Connected!<br><br>";
}
$last_name = "Lastname to search"; //You can post or get lastname in here.
/* create a prepared statement */
if ($sql = $connection->prepare("SELECT * FROM People WHERE `Last Name`=?")) {
/* bind parameters for markers */
$sql->bind_param("s", $last_name);
/* execute query */
$sql->execute();
/* instead of bind_result: */
$result = $sql->get_result();
/* close statement */
$sql->close();
}
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Street Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Email Address</th>
<th>Comment</th>
<th>Number of pets</th>
</tr>
</thead>
<tbody>
<?
/* now you can fetch the results into an array */
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td width=20><?=$row['iD'];?></td>
<td><?=$row['First Name'];?></td>
<td width=40><?=$row['First Name'];?></td>
<td width=200><?=$row['Street Address'];?></td>
<td width=30><?=$row['City'];?></td>
<td width=40><?=$row['State'];?></td>
<td width=30><?=$row['Zip Code'];?></td>
<td width=40><?=$row['Email Address'];?></td>
<td width=20><?=$row['Comment'];?></td>
<td width=10><?=$row['Number of pets'];?></td>
</tr>
<?
}
?>
</tbody>
</table>

Related

How can I dynamically make a query based on an HTML item ID?

So, I have this piece of HTML code which is just a navigation bar.
<div class="views">
<b>Views</b>
<hr style="background-color:#f0efef;">
<table id="views_table">
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_pending_cases">My Pending & On Hold Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_solved_cases">My Solved Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_csat_cases">My CSat Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><div>Escalation Review</div></td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><div>WW Support Tier 1</div></td>
</tr>
</table>
</div>
And then I have this php file which makes a query in my MySQL database and displays all the information in a table.
<?php
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM escalationreview";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
Now, what I want to achieve is: I want to call this php functiom everytime I click on an item in the navigation bar, then send the ID of the item clicked to the php function and query based on that ID, something like this:
HTML
<tr class="item">
<td class="view" id="WW Support Tier 1" onclick="fill(this.id)"><div>Escalation Review</div></td>
</tr>
PHP
<?php
function fill($id){
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
I know the above code does not work, it is just an idea of how I imagine this could work.
I believe this can be achieved using AJAX but I'm not completely sure. Any guidance here would be highly appreciated! Thanks beforehand.
If your PHP file renders the whole html, you might not need AJAX at all and just fill the link of each navigation item with the id as query parameter:
<td class="view" id="WW Support Tier 1">
<div>Escalation Review</div></td>
</tr>
Then in your PHP file, just get the ID as parameter
<?php
$id = $_REQUEST["id"] ?? null;
if($id) {
fill($id);
}
function fill($id) {
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
let formData = new FormData();
formData.append('id', this.id);
$.ajax({
url: "./yourPage.php",
type: "POST",
data:formData,
processData: false,
contentType: false,
complete: (response) => {
console.log(response.responseText)
}
})
in yourPage.php:
function fill($_REQUEST["id"]){ .... your code that you wrote above}

how to connect php echo with html table

I want to show mysql data in a html table... php echo shows me the content, but how can I transfer this into the html-table ? I've already added some dummy data manually into the html-table ..
<?php
$sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
}
} else {
echo "00 results";
}
$conn->close();
?>
<h1>Bestellungen</h1>
<div class="countdown" data-role="countdown" data-days="2"></div>
<div class="container" style="padding: 25px 0px;">
<table class="table striped hovered border" data-role="datatable" data-searching="true">
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<tr><td>2</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Filet</td><td>50</td></tr>
<tr><td>3</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Frisch</td><td>50</td></tr>
<tr><td>4</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Lebend</td><td>50</td></tr>
</tbody>
</table>
</div>
You should iterate inside the table and add the html come in echo
<div class="container" style="padding: 25px 0px;">
<table class="table striped hovered border" data-role="datatable" data-searching="true">
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["id"]. "</td>
<td>". $row["Datum"]. "</td>
<td>". $row["Kunde"] . "</td>
<td>". $row["Produkt"] . "</td>
<td>". $row["Produktversion"] . "</td>
<td>". $row["Menge"] ."</td>
</tr>";
}
?>
</tbody>
</table>
</div>
Remeber you have to loop each to create rows for the table.
$sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table>
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
//echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
?>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["Datum"] ?></td>
<td><?php echo $row["Kunde"]; ?></td>
<td>---</td>
<td>---</td>
<td>---</td>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo "00 results";
}
$conn->close();
You need to create each and every row inside the while loop.

How to make a PHP variable loop in template

I am trying to make something so it shows the database results. The problem is, it only shows 1 result. I want it to show multiple results using an quick and dirty template system. Also, is there a way to make my system better? Perhaps a class or a function? I need some insight on this. Thanks a bunch!
cp.php
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "********";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$activetournaments = "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
templates/cp.php
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</table>
I have changed $activetournaments to append to the end using '.='.
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td> ";
}
At the moment, for each record, it is overwriting $activetournaments. You could also use $activetournaments as an array then do a while / foreach in the template.
In your cp.php you overwrite $activetournaments in each while execution, change your code to:
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "1ShotGG Tournaments | Control Panel";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
$activetournaments = '';
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
In your templates/cp.php you can add a </tr> tag here:
<tr>
<?=$activetournaments?>
</tr>
your new templates/cp.php:
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</tr>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</table>

Don't display if no records exist in mysql

I have this form to search names in mysql database
<form action="search.php" method="GET">
<input type="text" placeholder="Search" name="name">
<input type="submit" value="Search">
this is the search.php
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Description</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
}
echo
"</tbody>
</table>";
?>
so there is no problems when I search for a name that exists in database it displays correctly, but the problem comes when I search for a name that doesn't exist in database.. I want it to display only
"No data available for that name specified" for the output but I will also see empty table in the output like this ------------> IMAGE..
so how can I get rid of the empty table for the output?
Just pu the if outside the table....
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
?>
change your if clause as below and remember to add exit() or die() function,this will end your php if there is no any data in database, and if there is any it will then start creating table for once and repeatedly fill up the table rows for given rows of data on database.
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
exit();
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
Move your echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
into the the if statement. This way it will only display the table when data is available!
$name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while ($row = mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>" . $name . "</td>
<td>" . $email . "</td>
<td>" . $desc . "</td>
</tr>";
}
echo "</tbody></table>";
}
Try this one. But don't forget to escape $_GET['name'] like htmlspecialchars and real_escape_string

Ajax responsetext as a table output?

I'm not really familiar with Ajax Response - I have edited the PHP Ajax Search code from W3schools.com as follows :
<?php
require_once('connect_db.php');
$query = "select item_no from items";
$result = mysql_query($query);
$a = array();
while ($row = mysql_fetch_assoc($result)){
$a[] = $row['item_no'];
}
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="No Suggestion";
}
else
{
$response=$hint;
}
//output the response
echo "<table border=1><tr><td>".$response."</td></tr></table>";
?>
The output of the above code works perfectly, but they are all listed like this (2L500BU , 2L500GO , 2L500NA , 2L500RD , 2L802CA , 2L802WH , 2L803GR , 2L804BE , 2L804BK , 2L804CO , 2L805BU , 2L806BE , 2L806GR ) - Those numbers are Item No in mysql table called items.
Now :
1) I want to output the response into table with <tr> for each like this
2l500BU
2L500GO
.
.
.
.
etc.
2) Do you think its possible to output all table records from Mysql based on the hint entered as follows :
$sql="SELECT * FROM items WHERE item_no = '".**$hint**."'";
$result = mysql_query($sql);
echo "<table align='center' cellpadding='3' cellspacing='3' width='800px' border='1' font style='font-family:arial;'>";
echo "
<tr align=center>
<th style=font-size:18px; bgcolor=#20c500>Item Number</th>
<th style=font-size:18px; bgcolor=#20c500>QTY</th>
<th style=font-size:18px; bgcolor=#20c500>Actual Price</th>
<th style=font-size:18px; bgcolor=#20c500>Selling Price</th>
<th style=font-size:18px; bgcolor=#20c500>Difference</th>
<th style=font-size:18px; bgcolor=#20c500>Date</th>
</tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr align=center bgcolor=#e3e3e3>";
echo "<td style='font-size:18px; font-weight:bold;'>" . strtoupper($row['item_no']) . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['qty'] . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['actual_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['discount_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['difference_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . date("d-m-Y",strtotime($row['date'])) . "</td>";
echo "</tr>";
}
echo "<table>";
If you're wanting to fetch items from a database and display a row for each of them, this is how you'd do it with jQuery.
Your PHP script:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$sql = "SELECT item_no FROM items";
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
$rows[] = $row['item_no'];
}
header('Content-Type: application/json');
echo json_encode($rows);
?>
And your HTML with the table:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th scope="col">Item No.</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
<script src="js/lib/jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON('yourscript.php', function(items) {
$.each(items, function(i, item) {
$('tbody').append('<tr><td>' + item + '</td></tr>);
});
});
});
</script>
I've also used MySQLi (MySQL improved) rather than the standard mysql_ functions, as the mysql_ library is deprecated and you should be using either MySQLi or PDO now.

Categories