How to not show the max and mini? - php

I want to show all the price but not the max and mini.
I'm using NOT IN but it's not working.
<?php $cod_product = $_GET["cod_product"];
$sql = "SELECT `ppm`
,`price`
,`market`
,`product`
,`name_market`
,`cod_market`
FROM ppm, markets
WHERE product=$cod_product AND cod_market=market NOT IN (SELECT MAX(price), MIN(price) FROM ppm)
ORDER BY price ASC";
$result = mysql_query($sql, $connection) or die("fail");
if ($dados = mysql_fetch_array($result)) {
do {
$cod_market = $dados['market'];
$nome_mercado = $dados['name_market'];
$price = $dados['price'];
echo $price;
echo $name_market;
}
while($dados = mysql_fetch_array($result));
}else { }
?>

Change mini() to min() (assuming MySQL).
mini() isn't a function.
Also, some of your SQL doesn't make much sense.
SELECT cod_ppm
,price
,market
,product
FROM ppm
WHERE price NOT IN (SELECT MAX(price), MIN(price) FROM ppm)
ORDER BY price ASC

Related

Calculate SUM in PHP

<?php
$today=date('Y-m-d');
$sorgu = mysqli_query($conn, "SELECT * FROM urun");
while ($sira = mysqli_fetch_array($sorgu)) {
$urun = $sira['urun'];
$query = mysqli_query($conn, "SELECT product_name, birim, SUM(quantity) FROM
product WHERE product_name = '$urun' AND quantity != '0' AND quantity > '0'
AND grup!='uygulama' AND skt > '$today' GROUP BY product_name, birim ");
while ($row = mysqli_fetch_array($query)){
if ($row['2'] <= $sira['minstok']) {
echo count($row['0']);
} } } ?>
This code belongs to a table which has a notification system. I want the notification part increase when the line number increases in that table but it turns 11111111... , So how can I make the code give me sum?
Incidentally, everything up to while ($row... can be rewritten as follows:
SELECT p.product_name
, p.birim
, SUM(p.quantity) total
FROM urun u
JOIN product p
ON p.product_name = u.urun
AND p.quantity > 0
AND p.grup != 'uygulama'
AND p.skt > CURDATE()
GROUP
BY p.product_name
, p.birim;

find the sum of value from array

I have a cart table and wish to calculate sum of cost wherever I have status=1
id cost status
1 10 1
2 10 1
2 10 1
2 10 2
the code that I tried is
$sql01 = "SELECT * FROM cart where status='1' ";
$result01 = mysqli_query($con, $sql01);
if (mysqli_num_rows($result01) > 0)
{
while($row = mysqli_fetch_assoc($result01))
{
$price = array_sum($row);
echo "sum of cod:"; echo $price;
echo "<br>";
}
}
the result that I am getting is
10
10
10
The result that should be is
30
One way to do it is to calculate the sum in SQL, as inother answer. If you want to do it in PHP, you can add the cost column to a variable:
$total_cost = 0;
while ($row = mysqli_fetch_assoc($result01)) {
$total_cost += $row['cost'];
}
echo "Result: $total_cost";
You are calculating the sum for each row. Instead you should try this:
$sql01 = "SELECT * FROM cart where status='1'";
$result01 = mysqli_query($con, $sql01);
$cost_array = array();
if (mysqli_num_rows($result01) > 0)
{
while($row = mysqli_fetch_assoc($result01))
$cost_array[] = $row['cost'];
}
echo "Result: ".array_sum($cost_array);
Or (better!) optimize your MySQL query this way:
SELECT sum(cost) finalprice FROM cart where status='1' GROUP BY status
Now you can access your sum using $row['finalprice'].
It's easier than your code: In SQL you should use
"Select sum(cost) from cart where status = '1';"
This is the query for you:
SELECT sum(cost) as price FROM cart WHERE status='1' GROUP BY status
PHP Code:
$sql01 = 'SELECT sum(cost) as price FROM cart WHERE status='1' GROUP BY status'
$result01 = mysqli_query($con, $sql01);
if (mysqli_num_rows($result01) > 0)
{
while($row = mysqli_fetch_assoc($result01))
{
echo 'sum of cod:' . $row['price'] . '<br>';
}
}
Read more about GROUP BY in:
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions-and-modifiers.html
Use this:
SELECT sum(cost) FROM cart group by status having status='1'

How to display default post with get method?

I want to get an id from browser and display some pictures from the database.
If there is no "display2.php?productid=" found, then I want to display default image.
How can I do that?
Here is my code;
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productid"].">";
}
else {
echo "<img src="SELECT productimage FROM productlist where productid = 1;">";
}
}
Now I will make it easier to explain for you...
Check this out;
//This part works without any problem
$sql = "SELECT * FROM productlista where productid=".$_GET['productid'];
$result = $mysqli->query($restwo);
while($myRow = $resulttwo->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productimage"].">";
}
//This part below (that should be default) does not work...
if (!$_GET){
echo "hello world"; }
Asaph pointed out SQL injection. You should bind the parameter (google it), or at the minimum do this:
$defaultImage = "SELECT * FROM productlist WHERE imageSrc != '' OR IS NOT NULL ORDER BY productid DESC LIMIT 1";
// run the query, get the result, create a variable with default image...
$defaultImageSrc = ''; // what you get from the query result
$_GET['productid'] = preg_replace('#[^0-9]#', '', $_GET['productid']);
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array()) {
if(!$myRow['imageSrc']) $myRow['imageSrc'] = $defaultImageSrc;
echo '<img src="'.$path.'">';
}
If you want either $_GET['productid'] or the max(productid) when $_GET['productid'] is not set, you can use a ternary to change your sql query
$productid = ! empty($_GET['productid']) ? " WHERE productid = ".(int)$_GET['productid'] : " ORDER BY productid DESC LIMIT 1";
$sql = "SELECT * FROM productlist".$productid
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
echo "<img src=".$myRow["productimage"].">";
}
so if isset($_GET['productid']) your query would be
SELECT * FROM productlist WHERE productid = (int)$_GET['productid']
but if not the default would be
SELECT * FROM productlist ORDER BY productid DESC LIMIT 1

mySQL Order by Most Commented and Least Commented

I'm trying to order a list of items based on the amount of comments for each topic as shown below:
$page = $_GET['page'];
$query = mysql_query("SELECT * FROM topic WHERE cat_id='$page' LIMIT $start, $per_page");
if (mysql_num_rows($query)>=1)
{
while($rows = mysql_fetch_array($query))
{
$number = $rows['topic_id'];
$title = $rows['topic_title'];
$description = $rows['topic_description'];
//get topic total
$sqlcomment = mysql_query("SELECT * FROM comments WHERE topic_id='$number'");
$commentnumber = mysql_num_rows($sqlcomment);
// TRYING TO ORDER OUTPUT ECHO BY TOPIC TOTAL ASC OR DESC
echo "
<ul>
<li><h4>$number. $title</h4>
<p>$description</p>
<p>$topictime</p>
<p>$commentnumber</p>
</li>
</ul>
";
}
}
else
{
echo "<p>no records available.</p><br>";
}
What would be the best way to order each echo by $num_rows (ASC/DESC values)? NOTE: I've updated with the full code - I am trying to order the output by $commentnumber
The first query should be:
SELECT t.*, COUNT(c.topic_id) AS count
FROM topic AS t
LEFT JOIN comments AS c ON c.topic_id = t.topic_id
WHERE t.cat_id = '$page'
GROUP BY t.topic_id
ORDER BY count
LIMIT $start, $per_page
You can get $commentnumber with:
$commentnumber = $rows['count'];
You don't need the second query at all.
First of all you have error here
echo "divs in order from least to greatest "number = $num_rows"";
It should be
echo "divs in order from least to greatest number = " . $num_rows . "";
And about the most commented try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY column DESC/ASC";
Or if there is not count column try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY COUNT(column) DESC/ASC";

How fetch more than one result from the table and use them in query

I have a problem with my code:
<?php
echo $s_country;
$sql2="SELECT prod_id FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
$i=0;
$j="";
while($rs=mysql_fetch_array($res)){
$j = $rs['prod_id'];
if(trim($s_country)=='US' || trim($s_country=='United States' )){
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id=".$j;
$res=mysql_query($sql3) or die(mysql_error());
$shipping_cost1=mysql_fetch_object($res);
}
$i++;
}
?>
What I actually want to do is to fetch the products id from the table tbl_order_item and with that products id select the shipping cost for those ids from the table tbl_product.
For example, if there is two product ids, it should select shipping cost for both the ids.
But here it only works for one product id like this:
SELECT shipping_us FROM tbl_product WHERE prod_id=526
But what I'm trying to do is :
SELECT shipping_us FROM tbl_product WHERE prod_id=526
SELECT shipping_us FROM tbl_product WHERE prod_id=527
you could use OR, like
SELECT shipping_us FROM tbl_product WHERE prod_id=526 OR prod_id=527
you can also use it like this with IN
SELECT shipping_us FROM tbl_product WHERE prod_id IN (526,527)
First Solution:
Change
$res=mysql_query($sql3) or die(mysql_error()); // you have overwritten result set of first query
$shipping_cost1=mysql_fetch_object($res);
To
$newres=mysql_query($sql3) or die(mysql_error());
$shipping_cost1=mysql_fetch_object($newres);
Second Solution (much better):
$sql2="SELECT GROUP_CONCAT(prod_id SEPARATOR ',') as pid FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res) > 0){
$rs=mysql_fetch_array($res);
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
while ($row = mysql_fetch_array($res)){
echo $row['shipping_us']
}
}
Recommendations:
1.Learn to prevent from MySQL Injections: Good Link
2.Mysql extension is not recommended for writing new code. Instead, either the mysqli or PDO_MySQL extension should be used. More reading: PHP Manual
my pblm was solved... and special tnx to GBD..
here is my final code...
<?php
echo $s_country;
$sql2="SELECT GROUP_CONCAT(prod_id SEPARATOR ',') as pid FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
$shipping_cost1 = 0;
if(mysql_num_rows($res) > 0){
$rs=mysql_fetch_array($res);
if(trim($s_country)=='US' || trim($s_country=='United States' )){
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
if(mysql_num_rows($res) > 0){
while ($row = mysql_fetch_array($res)){
$shipping_cost1 = $shipping_cost1 + $row['shipping_us'];
}
}
} else {
$sql3 = "SELECT shipping_outside FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
while ($row = mysql_fetch_array($res)){
$shipping_cost1= $shipping_cost1 + $row['shipping_outside'];
}
}
}
?>

Categories