Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So here I have a table of Products which contains ID, Name, Type & Price
Now I'm running a function to give me the minimal price of specific product types.
The datatype of Price is Price int(5,2)
function MinPrice($connection) {
echo "Minimum price: <br>";
$sql = "SELECT Type, min(Price)
FROM Products
GROUP BY Type;";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_assoc($result)) {
//Printing the result
echo $row['Type'].' || '.$row['Price'].'<br>';
}
}
The code works but not properly. I get the names of the product types but for the price, it gives me an error.
Undefined index: Price
Can anyone please help me out with this?
Try this:
$sql = "SELECT Type, min(Price) as Price
FROM Products
GROUP BY Type;";
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why is this working:
$sql_query = "SELECT * FROM Content WHERE id IN (1,5,7,9)";
But this isn't:
$array_values = "1,5,7,9";
$sql_query = "SELECT * FROM Content WHERE id IN ('$array_values')";
I want to select data from the database based on the integers in the $array_values string.
How can I do it?
because there are ` s in your code here
$sql_query = "SELECT * FROM Content WHERE id IN ('$array_values')";
use :
$sql_query = "SELECT * FROM Content WHERE id IN ( $array_values )";
or
$sql_query = "SELECT * FROM Content WHERE id IN (".$array_values.")";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am beginner in php. I am using core PHP. I want to make onkeyup filter in input box.I used some ajax for display the data in same page and i am trying these php code.
$text = $_POST['text'];
$result = mysqli_query($conn,"select * from project where name LIKE '%$text%' or type LIKE '%$text%' or sector LIKE '%$text%' or city LIKE '%$text%' or builder LIKE '%$text%' && status='1' LIMIT 6");
My filter is working but not comparing with status status=1. status=0 is deleted item and status=1 is active item. But in my filter box status=0 is also showing.
Please solve the problem. Your answer is highly appreciated.
Thanks
EDIT: I tried both &&, AND operator. Still not working
You have problem with syntax of mysql. Use (``) in the field of tables.
You should use parenthesis wit () to group between OR and AND.
This may help.
$result = mysqli_query($conn,"select * from project where ((`name` LIKE '%$text%') OR (`type` LIKE '%$text%') OR (`sector` LIKE '%$text%') OR (`city` LIKE '%$text%') OR (`builder` LIKE '%$text%')) AND `status`='1' LIMIT 6");
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Is there a simple explanation why this query doesn't work, and what is its alternative?
SELECT * FROM items WHERE item_category = 'shirts'
AND WHERE item_category = 'pants'
I have to keep the clause:
SELECT * FROM items
because I need all the data that is selected for later use.
Don't include WHERE twice:
SELECT * FROM items WHERE item_category = 'shirts' OR item_category = 'pants'
You also don't want to specify "AND", because there won't be a point where both item_category will be shirts and pants at the same time.
You have two WHERE keywords; the second is not necessary -- it generates an error.
However, you should simplify the query to use IN:
SELECT i.*
FROM items i
WHERE i.item_category IN ('shirts', 'pants');
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to show some data from a table in my database but i want it to show a specific ID "rid" that i get from another page with the code:
my SQL code is:
SELECT rid
, tid
, qid
, aid
, points
FROM result
WHEN rid = $val
if i delete WHEN rid=$val i will get all the all the "points" from my database. But i want to show a specific ID (rid). what should i do?
The correct SQL Statement would be
SELECT rid, tid, qid, aid, points FROM result WHERE rid=$val
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
$quantity=$_POST['quantity'];
do{
$request="SELECT id, name, type, price FROM `corro`.`food`";
$result=mysql_query($request);
}while(quantity>0)
First, you have a syntax error :
while (quantity > 0)
should be :
while ($quantity > 0)
But If you execute your code, you'll have an infinite loop if $quantity is actually greater than 0 : this will only execute your query over and over again because you never decrement $quantity.
I'm guessing that you have a quantity column in your table so what you should do is put the condition in your SQL query.
Using PDO that would give you :
$stmt = $dbh->prepare("SELECT id, name, type, price FROM food WHERE quantity > ?");
$stmt->execute(array($_POST['quantity']));
$results = $stmt->fetchAll();
EDIT (following the latest answers)
Note that mysql_* are deprecated as of PHP 5.5.0. You should use mysqli or PDO.
Based on your comment I assume it's this:
$quantity=$_POST['quantity'];
$request="SELECT id, name, type, price FROM `corro`.`food` WHERE quantity>0";
$result=mysql_query($request);
$quantity=$_POST['quantity'];
if($quantity>0){
$request="SELECT id, name, type, price FROM `corro`.`food`";
$result=mysql_query($request);
}
Just do it in your mysql_query
Please note that mysql_* functions are now deprecated, and should not be used. Use mysqli instead. The query for mysql would be SELECT id, name, type, price FROM corro.food WHERE quantity>0
Your while loop is running unlimited times try giving condition to your code as in below demo code
$quantity=$_POST['quantity'];
$quantity_count = 0;
do{
$request="SELECT id, name, type, price FROM `corro`.`food`";
$result=mysql_query($request);
$quantity_count++;
}while($quantity>0 && $quantity_count < 10); //This is demo code