How to debug a failing SELECT statement? [closed] - php

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');

Related

'Invalid paramater number' error for pdo query with regex [closed]

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 8 months ago.
Improve this question
I've been using PDO for years now, but today is my first time using REGEX in a query. I'm getting an odd error when I try to use PDO properly.
The original query with IN works.
$query = "SELECT * FROM tags AS t LEFT JOIN images AS i ON i.id=t.image WHERE t.tag IN(?)";
$args = array('apple');
$rslt = $pdo->prepare($query);
$rslt->execute($args);
I want to search for any instance and modification of the word, not just the word on its own, so I'm trying REGEXP. The same query with REGEXP and embedded arguments works.
$query = "SELECT * FROM tags AS t LEFT JOIN images AS i ON i.id=t.image WHERE t.tag REGEXP 'apple'";
$args = array();
$rslt = $pdo->prepare($query);
$rslt->execute($args);
When I try to move the argument to the $args array, I get the error 'Invalid paramater number'.
$query = "SELECT * FROM tags AS t LEFT JOIN images AS i ON i.id=t.image WHERE t.tag REGEXP '?'";
$args = array('apple');
$rslt = $pdo->prepare($query);
$rslt->execute($args);
Is this not allowed? I've seen others do it with bound values, but since I don't know how many words I'm going to be searching on, I don't really want to write code to bind each one.
Stupid mistake. It should be REGEXP ? not REGEXP '?'.

SQL not providing proper result. (Using PHP) [closed]

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;";

PHP: AND operator not working in mysql [closed]

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");

How to show data from a database with a requirement? [closed]

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

whats wrong with this mysql query in php? [closed]

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 8 years ago.
Improve this question
$sql="SELECT product.title, product.description, product.price, product.product_id,
FROM product
INNER JOIN (SELECT * FROM product_category WHERE category_id='$categoryid') AS a
ON a.product_id=product.product_id";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
and the i get this warning:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in......
You have a comma even after the last selected field.
Might be ambigous, try to change it to:
...
FROM product p INNER JOIN
...
and replace the product. prefix in the first part of your SELECT statement by p.
If this still does not work, copy the complete command to phpmyadmin and execute it there (replace $categoryid with a real value first), you usually get a clue as to what is wrong with your statement.
Also there are commands to return more information on mysql errors in php as well (mysql_error)

Categories