Skip to content

Instantly share code, notes, and snippets.

@edprince
Created August 20, 2014 12:43
Show Gist options
  • Save edprince/e3af255d57283b4f820e to your computer and use it in GitHub Desktop.
Save edprince/e3af255d57283b4f820e to your computer and use it in GitHub Desktop.
Code for PHP web page update from MySQL database
<?php
session_start()
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"
<link href="index.html">
<link href="displaydatatest.php">
<script>
//Go to W3Schools for example with "q"
function loadXMLDoc(int)
{
var xmlhttp;
var url = "displaydatatest.php?q="
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("displayData").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", url + int,true);
xmlhttp.send();
}
</script>
<!--Number of results form-->
<body>
<p class='title'>Presenting udp packet data</p><hr />
<center><div class='data-window'>
<form action="displaydata.php" method="GET" id="getSearchNumber">
<input type="text" name="NumberOfResults" id="noOfResults" placeholder="Number of desired results..." required="required" onkeyup="loadXMLDoc(this.value)" autocomplete="off">
</form>
<div id="displayData">
</div>
<?php
#$currentHighest = $_SESSION['currentHighest'];
#echo $currentHighest;
?>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
<link href="displaydata.php"></link>
</head>
<?php
//Set variable $noOfResults
//Display errors
ini_set('display_errors' ,1);
error_reporting(E_ALL);
if ($_GET['q'] !== "") {
try {
$noOfResults = $_GET["q"];
} catch(Exception $e) {
echo $e;
}
if (isset($noOfResults)) {
echo "";
} else {
echo"Variable setting failed";
}
$dbcnx = mysql_connect('localhost', 'myUsername', 'myPassword');
if (!$dbcnx) {
echo 'Unable to connect at this time...';
exit();
}
echo "";
#Select Database
mysql_select_db('udp', $dbcnx);
if (!@mysql_select_db('udp')) {
exit('Unable to locate the udp ' . 'database at this time.');
}
echo "";
#Set query
$result = mysql_query("SELECT * FROM data ORDER BY id DESC LIMIT $noOfResults");
if (!$result) {
exit('Error performing query: ' . mysql_error());
}
# $_SESSION['currentHighest'] = mysql_query("SELECT MAX(id) FROM data");
#Display results
while ($row = mysql_fetch_array($result)) {
echo '<p class="display-date">' . $row['date'] . '</p><p class="display">' . $row['id'] . ' - ' . base64_decode( $row['packetdata']) . '</p>';
echo "<hr align='center' width='90%' />";
}
echo "</div></center>";
}
?>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment