Helloo, I have been making a project in with I have to refresh the data every 10 seconds, I already created a separate file to load one info and it worked, but my question is, I have 3 sensors data on my database, Will I have to create 3 different refresh files to load each data to my main page?
This is one of my data files:
<?php
session_start();
include_once 'includes/dbh.inc.php';
$id = $_SESSION['userId'];
$dBname = "infosensor";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);
$sql = "SELECT * FROM `$id` ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
$ss1 = intval($row['sensor1'] * ($p = pow(10, 2))) / $p;
echo "".$ss1."A";
$s1 = $row['sensor1'];
}
}
?>
this is my update file:
<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
setInterval(function () {
$('#p0-blocoCorrente').load('load.php')
}, 5000);
});
</script>
this is how i diplay it:
<div class="blocoCorrente" id = "blocoCorrente">
<!-- Imprimir os valore dos sensor 1 -->
<div class="p0-blocoshow">Corrente 1:</div>
<div class="p0-blocoCorrente" id ="p0-blocoCorrente"></div>
</div>
UPDATE:
session_start();
include_once 'includes/dbh.inc.php';
$id = $_SESSION['userId'];
$sensor = isset($_POST['sensor']) ? "sensor" . intval($_POST['sensor']) : "sensor1";
$dBname = "infosensor";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);
$sql = "SELECT $sensor FROM `$id` ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if($row)
{
$s1 = $row[$sensor];
$ss1 = intval($s1 * ($p = pow(10, 2))) / $p;
echo $ss1;
}
$sensor1 = isset($_POST['sensor']) ? "sensor" . intval($_POST['sensor']) : "sensor2";
$sql = "SELECT $sensor1 FROM `$id` ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if($row)
{
$s2 = $row[$sensor1];
$ss2 = intval($s1 * ($p = pow(10, 2))) / $p;
echo $ss2;
}
Make the PHP script return all the sensors in a JSON object. Then you can display each of them in an appropriate DIV.
session_start();
include_once 'includes/dbh.inc.php';
$id = $_SESSION['userId'];
$dBname = "infosensor";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);
$sql = "SELECT sensor1, sensor2, sensor3 FROM `$id` ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$sensors = array();
if($row)
{
for ($i = 1; $i <= 3; $i++) {
$s1 = $row["sensor$i"];
$ss1 = intval($s1 * ($p = pow(10, 2))) / $p;
$sensors["sensor$i"] = $ss1 . "A";
}
echo json_encode($sensors);
} else {
echo json_encode(null);
}
Then change the JavaScript to use $.getJSON().
$(document).ready(function (){
setInterval(function () {
$.getJSON('load.php', function(sensors) {
if (sensors) {
$('#p0-blocoCorrente').text(sensors.sensor1);
$('#p1-blocoCorrente').text(sensors.sensor2);
$('#p2-blocoCorrente').text(sensors.sensor3);
}
});
}, 5000);
});
Related
I am Using Below Code to First Fetch all 'id' from MySql table.
<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "**";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$slug = $_GET["category"];
$sql = "SELECT * FROM table WHERE category = '1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dealid = $row["dealid"];
}} else {}
$conn->close();
?>
$dealid should return with all id's but it is returning with only 1.
Now below code is to show data with those id's:-
<?php
$num_rec_per_page=52;
mysql_connect('localhost','**','**');
mysql_select_db('**');
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM table2 WHERE id = $dealid ORDER BY id DESC LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql);
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/includes/dealbox.php' ); ?>
<?php
};
?>
But its only showing 1 data because returning id is only 1. I am not able to understand what the issue is. Any help is appreciable and will gift if someone help me out with this issue.
Dear friend all Id's are set into one array and return that array,
eg:
while ($row = mysql_fetch_assoc($rs_result))
{
$dealid[]= $row['id']; //array creation
}
and
return ($dealid);
This my PHP URL for fetching the data from MySQL. I need to make mysqli_fetch_array code saying if filled uid in the app-data table is the same with uid in users table fetch the data from all row in app-data to the uid like every user show his items from app-data table.
$host = "";
$user = "";
$pwd = "";
$db = "";
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM app_data ORDER By id";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
I think this is the correct answer:
<?php
$host = "";
$user = "";
$pwd = "";
$db = "";
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM app_data WHERE u_id=".$_POST['posted_uid']." ORDER By id";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
Little tip for the future:
Don't search exactly what you want, only search in parts.
I got little problem with my code... because I try to SELECT sth from database and then INSERT some value to another table, but in normal code from w3school
and I got error
Tryingo to get property of non-object
Here is my code:
<?php
session_start();
function connectionDB(){
$host = "localhost";
$username = "root";
$password = "";
$db_name = "project";
$conn = new mysqli($host, $username, $password, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
function getID(){
$id = $_GET['id'];
return $id;
}
$login =$_SESSION['login'];
$conn =connectionDB();
$idCar = getID();
echo $idCar;
$sqluser = "SELECT ID_USER FROM login_table WHERE LOGIN = $login";
if($result->num_rows > 0)
$result = $conn->query($sqluser);
{
while($row = $result->fetch_assoc())
{
$sql = "INSERT INTO cart (id_user) VALUES ('".$row['ID_USER']."')";
}
}else echo"error";
?>
THIS IS THE CODE OF SIDE WITH PRODUCT
Please see change near your IF loop
$sqluser = "SELECT ID_USER FROM login_table WHERE LOGIN = $login";
$result = $conn->query($sqluser); //check result first
if($result->num_rows > 0) //get number of rows
{
while($row = $result->fetch_assoc())
{
$sql = "INSERT INTO cart (id_user) VALUES ('".$row['ID_USER']."')";
}
}else echo"error";
What I am trying to do is simply display the row values. Now suppose if the field 'head_office' dont have the value 'H.O' then I want to display the values of the last row. I tried but cant find any solution. Here is my code: (I have only blocked the php part)
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '123';
$mysql_database = 'sdbms';
$setup_page = './myinstitute.php';
$db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $db);
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else if(!isset($_REQUEST['id'])){
$sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else{
$sql="SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n==0){
header('Location: '.$setup_page);
}
else{
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
?>
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '123';
$mysql_database = 'sdbms';
$setup_page = './myinstitute.php';
$db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $db);
$row = array();
if(isset($_REQUEST['id'])) {
$id = (int) $_REQUEST['id'];
if(!empty($id)) {
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
} else {
$sql = 'SELECT * FROM institute WHERE head_office = "H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
if(!isset($_REQUEST['id']) && empty($row))
$sql = "SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n == 0) {
header('Location: ' . $setup_page);
} else {
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
?>
As $_REQUEST['id'] can only have 2 status, isset and !isset, the else statement will never be used.
I don't understand very well how do you want to do, but it's illogic: the three step don't execute ever. Try it:
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else if(!isset($_REQUEST['id'])){
$sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
if(count($row)<=0) {
$sql="SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n==0){
header('Location: '.$setup_page);
}
else{
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
Enjoy your code.
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select database");
$id = 0;
if(isset($_GET['Day'])){ $id = (int)$_GET['Day']; }
if(!$id){
$query = "SELECT * FROM `TimeTable`";
} else {
$query = "SELECT * FROM `TimeTable` WHERE `Day`='".$id."'";
}
$result = mysql_query($query);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
print json_encode($rows);
?>
This code worked previously, but has now stopped, and is producing Parse error: syntax error, unexpected T_LOGICAL_OR in /Directory/TimeTable.php on line 27
I am also looking to add more parameters, (eg: Where Day = $id and Year = $Year )
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
or die(mysql_error());
This is a syntax error, doesn't know what to do with that or die() statement. Change to this:
$result = mysql_query($query);
if (!$result) {
die('Error');
}
while(...) {
}
I have looked up the new mysql functions an have changed to mysqli.
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database = "database";
$link = mysqli_connect($hostname, $username, $password, $database);
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
$id= 0;
if(isset($_GET['Day'])){ $id=(int)$_GET['Day']; }
$year = 0;
if(isset($_GET['Year'])){ $year=(int)$_GET['Year'];}
if(!$id){
$query = "SELECT * FROM TimeTable";
} else {
if (!year) {
$query = "Select * FROM TimeTable";
} else {
$query = "SELECT * FROM TimeTable WHERE Day=$id AND Year=$year";
}
}
$rows = array();
//Perform JSON encode
if($result = mysqli_query($link, $query)){
while($r = mysqli_fetch_assoc($result)){
$rows[] = $r;
}
}
print json_encode($rows);
?>