Memcached Result =0 - php

i have this code from my source
if ($where != "")
$where = "WHERE $where";
$res = $mysqli->query("SELECT COUNT(id) FROM torrents $where LIMIT 1") or die(mysql_error());
$row = mysqli_fetch_array($res);
$count = $row[0];
i try this is this corect? :
if ($where != "")
$where = "WHERE $where";
$ress = $mysqli->query('SELECT COUNT(id) FROM torrents $where LIMIT 1';) or die(mysql_error());
$nombre ='details'.$ress;
$res = mysqli_query($ress);
$count = array();
while($count = mysqli_fetch_array($res)) $rows[] = $count;
$memcache->set($nombre, $count, 1);
Result "sorry pal not found"

if ($where != "")
$where = "WHERE $where";
$res = mysql_query("SELECT COUNT(id) FROM torrents $where LIMIT 100") or die(mysql_error());
$key = md5($res);
$get_data = $memcache->get($key);
if($get_data) {
echo 'Data Pulled From Cache';
} else {
while ($row = mysql_fetch_array($res)) {
$memcache->set($key, $res, TRUE, 3600);
$count = $row[0];
}
}
While is resolved my prolem

Related

How to add values to in the result of while loop in php

How to add the result of the while loop which is stored on the variable $profit? results are 5 and 70
the code
<?php
$sql2 = "SELECT * from `products`";
$result2 = $link->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
$result3 = $link->query($sql3);
$row3 = $result3->fetch_assoc();
$sql = "SELECT SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
$result = $link->query($sql);
$row = mysqli_fetch_array($result);
$res = bcmul($row2['prod_price'], $row[0]);
$profit = $res - $row2['prod_cost'];
if($row[0] == null){
$row[0] = 0;
}
}?>
Try this code
<?php
$sql2 = "SELECT * from `products`";
$result2 = $link->query($sql2);
$totalProfit = 0;
while($row2 = $result2->fetch_assoc())
{
$sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
$result3 = $link->query($sql3);
$row3 = $result3->fetch_assoc();
$sql = "SELECT SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
$result = $link->query($sql);
$row = mysqli_fetch_array($result);
$res = bcmul($row2['prod_price'], $row[0]);
$profit = $res - $row2['prod_cost'];
$totalProfit = $totalProfit + $profit
if($row[0] == null){
$row[0] = 0;
}
}?>
now you can use $totalProfit variable value as total.

How to read data from database change them and update database

I need to read data from database then edit these data with php and update database with these data.
I can get data from database but not able to write them back (I add another query to see if value is changed)
I’ve tried to use phpMyAdmin to see if values are changed but they are stil
same
$POWER = "";
if ($pressed == "1") {
echo "pressed\n";
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "pressed power request $row[0]\n";
}
if ($POWER == "ON") {
$sql = 'UPDATE `VolleyTest` SET `POWER`=\"OFF\" WHERE ID = 1';
$conn->query($sql);
echo " POWER ON to OFF\n";
} else if ($POWER == "OFF") {
$sql = 'UPDATE `VolleyTest` SET `POWER`=\"ON\" WHERE ID = 1';
$conn->query($sql);
echo " POWER OFF to ON\n";
}
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "$row[0]\n";
}
echo $POWER; //. " ". $pressed . " " . $_POST['key'];
You dont need to escape double quotes inside a single quoted string.
Doing that generates a string like this
UPDATE `VolleyTest` SET `POWER`=\"ON\" WHERE ID = 1
So this may work
$POWER = "";
if ($pressed == "1") {
echo "pressed\n";
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "pressed power request $row[0]\n";
}
if ($POWER == "ON") {
$sql = 'UPDATE `VolleyTest` SET `POWER`="OFF" WHERE ID = 1';
$conn->query($sql);
echo " POWER ON to OFF\n";
} else if ($POWER == "OFF") {
$sql = 'UPDATE `VolleyTest` SET `POWER`="ON" WHERE ID = 1';
$conn->query($sql);
echo " POWER OFF to ON\n";
}
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "$row[0]\n";
}
echo $POWER; //. " ". $pressed . " " . $_POST['key'];

mysql SELECT WHERE query inside elseif loop not doing anything and no error

I run my script ( see below ) but the place where industry & keywordrw vars are set it just doesn't do anything
The other queries where just the industry is set works fine
the other 2 that involve doing multiple LIKE queries on the keyword variable don't do anything
<?php
if (!isset($industry)) {
$industry = '';
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw;
} elseif (!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $industry;
} elseif (isset($keyword) && ($industry)) {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw . '-' . $industry;
} else {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}
?>
Changed a few things and updated working code is below
if(!isset($industry))
{
$industry = '';
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE title LIKE '%keywordrw%'",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(isset($keyword) &&($industry)){
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} else {
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}

Multiple mysqli select with json

<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "SELECT id from diseases";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
while ($row = mysql_fetch_array($result_pag_data)) {
$diseases_id = $row['id'];
$result = mysqli_query($con,"SELECT $diseases_id FROM treatment WHERE gender_id = '10' AND diseases_id = '$diseases_id'");
$row_gid = mysqli_fetch_array($result);
if ($row_gid == TRUE){
$sl_dise = mysqli_query($con,"SELECT Diseases_type, id FROM diseases WHERE id = '$diseases_id'");
$rowss = array();
while($r = mysqli_fetch_assoc($sl_dise)) {
$rowss[] = $r;
}
print json_encode($rowss);
}
}
Output is:
[{"Diseases_type":"fever","id":"114"}][{"Diseases_type":"rhrh","id":"123"}]
How can i get this output:
[{"Diseases_type":"fever","id":"114"},{"Diseases_type":"rhrh","id":"123"}]
You need to initialize the JSON array before the outer loop, and print it at the very end, not each time through.
<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "SELECT id from diseases";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$rowss = array();
while ($row = mysql_fetch_array($result_pag_data)) {
$diseases_id = $row['id'];
$result = mysqli_query($con,"SELECT $diseases_id FROM treatment WHERE gender_id = '10' AND diseases_id = '$diseases_id'");
$row_gid = mysqli_fetch_array($result);
if ($row_gid == TRUE){
$sl_dise = mysqli_query($con,"SELECT Diseases_type, id FROM diseases WHERE id = '$diseases_id'");
while($r = mysqli_fetch_assoc($sl_dise)) {
$rowss[] = $r;
}
}
}
print json_encode($rowss);
Here is you optimised code
<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "select diseases.Diseases_type,diseases.id from diseases inner join treatment on treatment.diseases_id = diseases.id where treatment.gender_id = '10' ";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$rowss = array();
while ($row = mysqli_fetch_assoc($result_pag_data)) {
$rowss[] = $row;
}
print json_encode($rowss);
?>
Put your
print json_encode($rowss);
after end of thiswhile ($row = mysql_fetch_array($result_pag_data)) {
Also remove this $rowss = array(); array initialization

How to display user avatar next to post? (php,mysql)

So I have two tables in my database one is called Users and the other is News
I made it that users can add News Posts to the site, but I couldn't display user's image next to his post
this is my code right now
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src=\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
I want where is says $row2['author_avatar'] to echo the image from the users table
You missed out a speech mark (") just before $row2['author_avatar'].
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src="\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
You should look up INNER JOIN rather than having two SQL queries as mentioned by andrewsi. Here's a good tutorial on how to use it https://www.youtube.com/watch?v=6BfofgkrI3Y.

Categories