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);
Related
I'm fetching my songs from MySQL database and I'll be adding more song in the futures so I can't hard coded the max number so I'll be really appreciated if I can get any help or suggestion how to do it.
you can use the following query to get the total records in DB
$query = 'SELECT count(*) as total FROM song';
for checking if the max is reached
<? if ($page*$recordsPerPage < $total) : ?>
Next
complete example for hiding the next
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!isset($_GET['id'])) {
$id = 1;
} else {
$id = (int)$_GET['id'];
}
$recordsPerPage = 10 ;
// $query = 'SELECT * FROM song WHERE 1 LIMIT ' . (($id - 1) * $recordsPerPage) . ' ' . $recordsPerPage;
//Here need to handle the songs data retreive
$sql = "select count(*) as total FROM songs";
$result = $conn->query($sql);
$data = $result->fetch_assoc();
if ($id*$recordsPerPage < (int)$data["total"])
echo "<a href='page/details.php?id=". ($id+1) ."'>Next</a>";
$conn->close();
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);
});
I know this question has been asked alot of times earlier, but none of the other answers worked for me. I'm having trouble on this line:
$row = $conn->query("SELECT * FROM urls WHERE id = '$id'");
I followed a tutorial so I don't know if there is any other information that I should provide
EDIT:
heres the whole text document:
<?php
function idExists($id){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT * FROM urls WHERE id = '.$id'");
if($row -> num_rows > 0){
return true;
} else {
return false;
}
}
function urlHasBeenShortened($url){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT * FROM urls WHERE link_to_page = '$url'");
if($row->num_rows > 0){
return true;
} else {
return false;
}
}
function getURLID($url){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT id FROM urls WHERE link_to_page = '$url'");
return $row->fetch_assoc()['id'];
}
function insertID($id, $url){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$conn->query("INSERT INTO urls (id, link_to_page) VALUES ('$id', '$url')");
if(strlen($conn->error) == 0){
return true;
}
}
function getUrlLocation($id){
include $_SERVER['DOCUMENT_ROOT'] . '/short/includes/init.php';
$row = $conn->query("SELECT link_to_page FROM urls WHERE id = '$id'");
return $row->fetch_assoc()['link_to_page'];
}
?>
Init code
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
errors on lines: 7, 18
you did forget the database name :
$conn = new mysqli($servername, $username, $password);
// Create connection like this :
$conn = new mysqli($servername, $username, $password, $dbname);
change
$row = $conn->query("SELECT * FROM urls WHERE id = '$id'");
to
$row = $conn->query("SELECT * FROM urls WHERE id = ".$id);
also change :
if($row -> num_rows > 0)
to
if($row->num_rows > 0)
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";