whats wrong with this mysql query in php? [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 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)

Related

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

Inner Join Error joining two tables - small syntax error I'm assuming [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
I have a tickets table and a customers table. I'm trying to take basic information from the tickets table, and simply add the clientDisplayName field from the customers table that corresponds to the the clientID field in the ticket table.
I also need to verify whether the where clause is right, I only want it to return ticket info where the ticketstatus IS NOT EQUAL to "closed" Should I use <> or IS NOT EQUAL TO?
I'm getting a #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"SELECT t.id, t.clientID, c.clientDisplayName, t.ticketTitle t.ticketDueDate t.t' at line 1
Here's my query, I just started using PDO, but I believe my issue is just with the join as I'm new to that also:
$conn->prepare("SELECT
t.id,
t.clientID,
c.clientDisplayName,
t.ticketTitle,
t.ticketDueDate,
t.ticketStatus,
t.ticketProblemType,
t.ticketAssignedTech,
t.ticketLastModifiedDate FROM `tickets` AS t
INNER JOIN `customers` AS c ON t.clientID = c.id
WHERE t.ticketStatus <> "closed"
ORDER BY t.ticketDueDate ASC");
Missing , after column names starting from t.ticketTitle.
You are using " as your string for the prepare so you need to put ' around closed so it doesn't think you a closing the original string. E.g. WHERE t.ticketStatus <> 'closed'
For each column you are selecting the statement should be deliminated by a ,. In addition, there are other syntax and semantic issues that are present, consult the error codes as they come and reference the docs. Before creating a join, I recommend getting the table formed separately so you can verify the tables are correct then do an INNER JOIN. Building a query is a step by step process.
In MySQL comparing strings should use LIKE or NOT LIKE
MySQL Doc on String Comparison
For reference, if you need additional syntax for queries consult the MySql Doc

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

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.

Categories