PHP: AND operator not working in mysql [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 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");

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 query doesnt find exact name from database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
SELECT * FROM hge_funcionarios
JOIN hospitais
ON hge_funcionarios.hospital_id = hospitais.id_hospitais
JOIN funcoes
ON hge_funcionarios.funcao_id = funcoes.id_funcoes
WHERE nome LIKE '%$search%'
ORDER BY hospital_id DESC
When I try the exact name from the database doesnt show up any results.
If i search "Larissa" or "LARISSA", I get no results even in my database having "LARISSA CAMPOS".
If I try "lar" or anything like this I can find it, but when it gets too close to the name on database like "LARISS" I can't find it any more.
I tried collate and charset but no success.
EDIT: Its not a Query error with ambiguous column name in SQL because column names are distinct.
I'm writing this answer since it's not possible to show it in the comments. Feel free to disregard it.
The problem you are facing seems to be related to the injection of parameter values into your SQL query. The easy (dangerous) way is to simply concatenate strings, as in:
$stmt = $conn->prepare(
"select * from my_table where name = '" . $param1 . "'");
Even though it works for simple cases, your case is more complicated, and confusing. Most of the time you'll use Prepared Statements as in:
$stmt = $conn->prepare("select * from my_table where name = ?");
$stmt->bind_param("sss", $param1);
This way, the parameter will be injected the right way. In your case you'll need to prepend and append % to your parameter, since it'll be used for a LIKE operator.
WHERE nome LIKE '%$search%'
May be $ is the Reason.Try Like : WHERE nome LIKE '%search%'

How to debug a failing SELECT statement? [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 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');

php report undefined index when used select * in mysql [closed]

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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have problem only if I use select * but if I select exact field from my database it is working fine
$sql = "SELECT * FROM `product id`;";
$resutl = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
echo $row["product name"];
};
It is working if I use
SELECT `product name` FROM `product id`
Thank you
$sql = "SELECT * FROM `product id`";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
print_r($row);
}
and check your array and traverse it perfectly as when you call all rows it will not be same as fetching one row.
Try this it should work fine, and you will get more idea.
only 2 errors I found is $result variable was not correct and semicolon in query!
Table names e column names that include white-space are not a good idea because may be in conflict with mysql sintax (when mysql parse the query). You can use var_dump($row).
Use mysql_fetch_array() instead of mysql_fetch_assoc()
While the two are similar mysql_fetch_assoc() only returns an associative array.
Also you should think of moving from mysql to mysqli or PDO. mysql is being removed as of PHP6 and already depreciated as of PHP5.5.

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