P粉8795174032023-08-26 12:59:05
First, you have to accept the fact that if your query doesn't find any rows, that means there is no match , even if you can swear that the data is correct eh>. When a query returns no rows, it means that there are no rows that match the criteria. So you have to find out - why. But first you need to make sure your query is correct:
First, you need to make sure that your query actually runs without errors, as "no results" may mean that there is an error in the query. See these answers for details: pdo and mysqli.
Check your conditions. There are mutually exclusive conditions, such as WHERE col=1 AND col=2
. It never returns any rows. Try simplifying the condition until it starts returning some rows, then refine the condition to get the results you want.
But okay, there are no errors, the conditions are correct, and you could swear the data in the table matches your query. Still, there are some pitfalls:
First, if a variable is involved, make sure it exists and actually contains some value.
Then check the value itself. There may be some converted or unprintable characters in the input data (or database). Such as line breaks or specially encoded symbols, or certain characters that are converted to HTML entities, such as <<
and >
. Therefore, a query containing
will never match the text
. For a quick check, you can use the rawurlencode()
function, which converts all non-Latin characters into codes, thus making them visible.
The problem is, this is just a guess and no one can tell you what the actual problem is because it is your database, your input data and only < strong>you can find the problem.
I wrote an article explaining how to debug PDO issues.
To debug a specific issue you need
urlencode()
function can be helpful, displaying all non-printable and convertible characters in the database and input. Another common problem is when you have multiple databases and connect to the wrong database that does not contain the requested data. This question is similar to this question, so just follow the same routine and only check the data rows instead of the table list.
This is a rare situation, but to be sure, follow the checklist in this Good Answer