Home  >  Q&A  >  body text

Having problems matching rows in the database

I want to get the row count to check if the same email already exists in the database. I tried several mechanisms without success. When I run the query directly in the database it gives the number of rows but executing it through PDO gives 0.

I have used the fetchAll method to count manually and even using the rowCount method does not work

$sql = 'SELECT count(*) FROM inbox WHERE uid = ? AND from_email = ?'; 
$result = $link->prepare($sql); 
$result->execute([$email_number,$email_f]); 
$number_of_rows = $result->fetchColumn();

The problem is with this $email_f, which contains html

SELECT count(*) FROM inbox WHERE uid = "6961" 
AND from_email = "abc Offers <abc@abcs.com>"

This is the query I printed from $sql and when I execute it directly in the database in phpmyadmin it works fine. I'm given the number 3, but by executing I get 0.

P粉464208937P粉464208937230 days ago386

reply all(1)I'll reply

  • P粉547170972

    P粉5471709722024-03-27 00:16:59

    First of all, you have to accept the fact that if your query does not find any rows, it means there is no match, even if you can swear that the data is correct. 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:

    Problems caused by SQL errors

    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.

    If the error says "No such table/database", see the "Connection Credentials" case below.

    Problems caused by the situation

    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:

    Problems caused by data

    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). For example, line breaks or specially encoded symbols, or certain characters that are converted to HTML entities, such as < and >. Therefore, a query containing <abc@abcs.com> will never match the text <abc@abcs.com>. 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 problem you need

    • Make sure that both PDO and PHP have full error reporting enabled. It's really helpful and can show you the occasional typographical errors, spelling errors, etc.
    • Double check the data and input in the database for discrepancies. The urlencode() function can be helpful, displaying all non-printable and convertible characters in the database and input.

    Problems caused by connection credentials

    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.

    Problems caused by character set/encoding

    This is a rare situation, but to be sure, follow the checklist in this Good Answer

    reply
    0
  • Cancelreply