Why can't MySql see the duplicate? [duplicate] - php

I am looking to get row count to check if same email is already in database or not. i have tried couple of mechanism but no success. when i run my query directly in the database it gives me the row count but via PDO execute it gives me 0.
i have used fetchAll method to manually count, even used rowCount method that also not working
$sql = 'SELECT count(*) FROM inbox WHERE uid = "'.$email_number.'" AND from_email = "'.$email_f.'"';
$result = $link->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
issue is with this $email_f, it contains html
SELECT count(*) FROM inbox WHERE uid = "6961"
AND from_email = "abc Offers <abc#abcs.com>"
this is the query which i have printed from $sql and when i execute it in database directly in phpmyadmin, it works fine. give me count of 3 but via execute i get 0.

First of all, you have to embrace the fact: if your query found no rows, it means there is no match, even if you can swear the data is all right. When the query returns no rows, then there are no rows to match the condition. So you have to find out - why. But first of all you need to make sure that your query is all right:
Problems caused by SQL errors
First of all you need to make sure that your query actually runs without errors as "no result" could mean an error in the query. Refer to these answers for the details: pdo and mysqli.
Problems caused by the condition
Check your conditions. There are mutual exclusive conditions, such as WHERE col=1 AND col=2. It will never return any rows. Try to simplify the condition until it starts returning some rows, and then refine the conditions to get the desired result.
But all right, there are no errors, conditions are correct, and you can swear there is data in the table to match your query. Still, there are some pitfalls:
Problems caused by the data
Most likely there are some converted or non-printable characters in the input data (or database). For example, there could be a linefeed character or a peculiarly encoded symbol, or some characters such as < and > converted into HTML entities. As a result, the query contains <abc#abcs.com> will never match a text <abc#abcs.com>.
The problem is, this is only a guess, and nobody can tell you what the actual issue is, because it is your database, your input data and only you can find the issue.
I wrote an article that explains how to debug your PDO issues.
To debug a particular issue, you need
make sure the full error reporting is enabled for both PDO and PHP. It really helps, showing you occasional typographic errors, spelling errors and the such
scrutinize both the data in the database and the input to find the difference. bin2hex() function would help, revealing all non-printable and converted characters, in both database and the input.
Problems caused by the connection credentials
Another frequent issue is when you have several databases and connect to the wrong one that doesn't have the data requested. This issue is similar to this one, so just follow the same routine, only checking not the list of tables but the data rows.
Problems caused by character set/encoding
It's a rare case, but just to to be sure, follow the checklist from this great answer
An irrelevant but important note
On a side note, but very important nevertheless: your prepared statement is a cargo cult code that protects nothing. Here is how it must be:
$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();

Related

PDO string comparision fails programatically [duplicate]

I am looking to get row count to check if same email is already in database or not. i have tried couple of mechanism but no success. when i run my query directly in the database it gives me the row count but via PDO execute it gives me 0.
i have used fetchAll method to manually count, even used rowCount method that also not working
$sql = 'SELECT count(*) FROM inbox WHERE uid = "'.$email_number.'" AND from_email = "'.$email_f.'"';
$result = $link->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
issue is with this $email_f, it contains html
SELECT count(*) FROM inbox WHERE uid = "6961"
AND from_email = "abc Offers <abc#abcs.com>"
this is the query which i have printed from $sql and when i execute it in database directly in phpmyadmin, it works fine. give me count of 3 but via execute i get 0.
First of all, you have to embrace the fact: if your query found no rows, it means there is no match, even if you can swear the data is all right. When the query returns no rows, then there are no rows to match the condition. So you have to find out - why. But first of all you need to make sure that your query is all right:
Problems caused by SQL errors
First of all you need to make sure that your query actually runs without errors as "no result" could mean an error in the query. Refer to these answers for the details: pdo and mysqli.
Problems caused by the condition
Check your conditions. There are mutual exclusive conditions, such as WHERE col=1 AND col=2. It will never return any rows. Try to simplify the condition until it starts returning some rows, and then refine the conditions to get the desired result.
But all right, there are no errors, conditions are correct, and you can swear there is data in the table to match your query. Still, there are some pitfalls:
Problems caused by the data
Most likely there are some converted or non-printable characters in the input data (or database). For example, there could be a linefeed character or a peculiarly encoded symbol, or some characters such as < and > converted into HTML entities. As a result, the query contains <abc#abcs.com> will never match a text <abc#abcs.com>.
The problem is, this is only a guess, and nobody can tell you what the actual issue is, because it is your database, your input data and only you can find the issue.
I wrote an article that explains how to debug your PDO issues.
To debug a particular issue, you need
make sure the full error reporting is enabled for both PDO and PHP. It really helps, showing you occasional typographic errors, spelling errors and the such
scrutinize both the data in the database and the input to find the difference. bin2hex() function would help, revealing all non-printable and converted characters, in both database and the input.
Problems caused by the connection credentials
Another frequent issue is when you have several databases and connect to the wrong one that doesn't have the data requested. This issue is similar to this one, so just follow the same routine, only checking not the list of tables but the data rows.
Problems caused by character set/encoding
It's a rare case, but just to to be sure, follow the checklist from this great answer
An irrelevant but important note
On a side note, but very important nevertheless: your prepared statement is a cargo cult code that protects nothing. Here is how it must be:
$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();

Sql query in php not returning the value [duplicate]

I am looking to get row count to check if same email is already in database or not. i have tried couple of mechanism but no success. when i run my query directly in the database it gives me the row count but via PDO execute it gives me 0.
i have used fetchAll method to manually count, even used rowCount method that also not working
$sql = 'SELECT count(*) FROM inbox WHERE uid = "'.$email_number.'" AND from_email = "'.$email_f.'"';
$result = $link->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
issue is with this $email_f, it contains html
SELECT count(*) FROM inbox WHERE uid = "6961"
AND from_email = "abc Offers <abc#abcs.com>"
this is the query which i have printed from $sql and when i execute it in database directly in phpmyadmin, it works fine. give me count of 3 but via execute i get 0.
First of all, you have to embrace the fact: if your query found no rows, it means there is no match, even if you can swear the data is all right. When the query returns no rows, then there are no rows to match the condition. So you have to find out - why. But first of all you need to make sure that your query is all right:
Problems caused by SQL errors
First of all you need to make sure that your query actually runs without errors as "no result" could mean an error in the query. Refer to these answers for the details: pdo and mysqli.
Problems caused by the condition
Check your conditions. There are mutual exclusive conditions, such as WHERE col=1 AND col=2. It will never return any rows. Try to simplify the condition until it starts returning some rows, and then refine the conditions to get the desired result.
But all right, there are no errors, conditions are correct, and you can swear there is data in the table to match your query. Still, there are some pitfalls:
Problems caused by the data
Most likely there are some converted or non-printable characters in the input data (or database). For example, there could be a linefeed character or a peculiarly encoded symbol, or some characters such as < and > converted into HTML entities. As a result, the query contains <abc#abcs.com> will never match a text <abc#abcs.com>.
The problem is, this is only a guess, and nobody can tell you what the actual issue is, because it is your database, your input data and only you can find the issue.
I wrote an article that explains how to debug your PDO issues.
To debug a particular issue, you need
make sure the full error reporting is enabled for both PDO and PHP. It really helps, showing you occasional typographic errors, spelling errors and the such
scrutinize both the data in the database and the input to find the difference. bin2hex() function would help, revealing all non-printable and converted characters, in both database and the input.
Problems caused by the connection credentials
Another frequent issue is when you have several databases and connect to the wrong one that doesn't have the data requested. This issue is similar to this one, so just follow the same routine, only checking not the list of tables but the data rows.
Problems caused by character set/encoding
It's a rare case, but just to to be sure, follow the checklist from this great answer
An irrelevant but important note
On a side note, but very important nevertheless: your prepared statement is a cargo cult code that protects nothing. Here is how it must be:
$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();

PDO based query with space in criteria string not working in PHP [duplicate]

I am looking to get row count to check if same email is already in database or not. i have tried couple of mechanism but no success. when i run my query directly in the database it gives me the row count but via PDO execute it gives me 0.
i have used fetchAll method to manually count, even used rowCount method that also not working
$sql = 'SELECT count(*) FROM inbox WHERE uid = "'.$email_number.'" AND from_email = "'.$email_f.'"';
$result = $link->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
issue is with this $email_f, it contains html
SELECT count(*) FROM inbox WHERE uid = "6961"
AND from_email = "abc Offers <abc#abcs.com>"
this is the query which i have printed from $sql and when i execute it in database directly in phpmyadmin, it works fine. give me count of 3 but via execute i get 0.
First of all, you have to embrace the fact: if your query found no rows, it means there is no match, even if you can swear the data is all right. When the query returns no rows, then there are no rows to match the condition. So you have to find out - why. But first of all you need to make sure that your query is all right:
Problems caused by SQL errors
First of all you need to make sure that your query actually runs without errors as "no result" could mean an error in the query. Refer to these answers for the details: pdo and mysqli.
Problems caused by the condition
Check your conditions. There are mutual exclusive conditions, such as WHERE col=1 AND col=2. It will never return any rows. Try to simplify the condition until it starts returning some rows, and then refine the conditions to get the desired result.
But all right, there are no errors, conditions are correct, and you can swear there is data in the table to match your query. Still, there are some pitfalls:
Problems caused by the data
Most likely there are some converted or non-printable characters in the input data (or database). For example, there could be a linefeed character or a peculiarly encoded symbol, or some characters such as < and > converted into HTML entities. As a result, the query contains <abc#abcs.com> will never match a text <abc#abcs.com>.
The problem is, this is only a guess, and nobody can tell you what the actual issue is, because it is your database, your input data and only you can find the issue.
I wrote an article that explains how to debug your PDO issues.
To debug a particular issue, you need
make sure the full error reporting is enabled for both PDO and PHP. It really helps, showing you occasional typographic errors, spelling errors and the such
scrutinize both the data in the database and the input to find the difference. bin2hex() function would help, revealing all non-printable and converted characters, in both database and the input.
Problems caused by the connection credentials
Another frequent issue is when you have several databases and connect to the wrong one that doesn't have the data requested. This issue is similar to this one, so just follow the same routine, only checking not the list of tables but the data rows.
Problems caused by character set/encoding
It's a rare case, but just to to be sure, follow the checklist from this great answer
An irrelevant but important note
On a side note, but very important nevertheless: your prepared statement is a cargo cult code that protects nothing. Here is how it must be:
$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();

PHP pdo only gets result when query is hardcoded [duplicate]

I am looking to get row count to check if same email is already in database or not. i have tried couple of mechanism but no success. when i run my query directly in the database it gives me the row count but via PDO execute it gives me 0.
i have used fetchAll method to manually count, even used rowCount method that also not working
$sql = 'SELECT count(*) FROM inbox WHERE uid = "'.$email_number.'" AND from_email = "'.$email_f.'"';
$result = $link->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
issue is with this $email_f, it contains html
SELECT count(*) FROM inbox WHERE uid = "6961"
AND from_email = "abc Offers <abc#abcs.com>"
this is the query which i have printed from $sql and when i execute it in database directly in phpmyadmin, it works fine. give me count of 3 but via execute i get 0.
First of all, you have to embrace the fact: if your query found no rows, it means there is no match, even if you can swear the data is all right. When the query returns no rows, then there are no rows to match the condition. So you have to find out - why. But first of all you need to make sure that your query is all right:
Problems caused by SQL errors
First of all you need to make sure that your query actually runs without errors as "no result" could mean an error in the query. Refer to these answers for the details: pdo and mysqli.
Problems caused by the condition
Check your conditions. There are mutual exclusive conditions, such as WHERE col=1 AND col=2. It will never return any rows. Try to simplify the condition until it starts returning some rows, and then refine the conditions to get the desired result.
But all right, there are no errors, conditions are correct, and you can swear there is data in the table to match your query. Still, there are some pitfalls:
Problems caused by the data
Most likely there are some converted or non-printable characters in the input data (or database). For example, there could be a linefeed character or a peculiarly encoded symbol, or some characters such as < and > converted into HTML entities. As a result, the query contains <abc#abcs.com> will never match a text <abc#abcs.com>.
The problem is, this is only a guess, and nobody can tell you what the actual issue is, because it is your database, your input data and only you can find the issue.
I wrote an article that explains how to debug your PDO issues.
To debug a particular issue, you need
make sure the full error reporting is enabled for both PDO and PHP. It really helps, showing you occasional typographic errors, spelling errors and the such
scrutinize both the data in the database and the input to find the difference. bin2hex() function would help, revealing all non-printable and converted characters, in both database and the input.
Problems caused by the connection credentials
Another frequent issue is when you have several databases and connect to the wrong one that doesn't have the data requested. This issue is similar to this one, so just follow the same routine, only checking not the list of tables but the data rows.
Problems caused by character set/encoding
It's a rare case, but just to to be sure, follow the checklist from this great answer
An irrelevant but important note
On a side note, but very important nevertheless: your prepared statement is a cargo cult code that protects nothing. Here is how it must be:
$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();

Having issue with matching rows in the database

I am looking to get row count to check if same email is already in database or not. i have tried couple of mechanism but no success. when i run my query directly in the database it gives me the row count but via PDO execute it gives me 0.
i have used fetchAll method to manually count, even used rowCount method that also not working
$sql = 'SELECT count(*) FROM inbox WHERE uid = "'.$email_number.'" AND from_email = "'.$email_f.'"';
$result = $link->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
issue is with this $email_f, it contains html
SELECT count(*) FROM inbox WHERE uid = "6961"
AND from_email = "abc Offers <abc#abcs.com>"
this is the query which i have printed from $sql and when i execute it in database directly in phpmyadmin, it works fine. give me count of 3 but via execute i get 0.
First of all, you have to embrace the fact: if your query found no rows, it means there is no match, even if you can swear the data is all right. When the query returns no rows, then there are no rows to match the condition. So you have to find out - why. But first of all you need to make sure that your query is all right:
Problems caused by SQL errors
First of all you need to make sure that your query actually runs without errors as "no result" could mean an error in the query. Refer to these answers for the details: pdo and mysqli.
Problems caused by the condition
Check your conditions. There are mutual exclusive conditions, such as WHERE col=1 AND col=2. It will never return any rows. Try to simplify the condition until it starts returning some rows, and then refine the conditions to get the desired result.
But all right, there are no errors, conditions are correct, and you can swear there is data in the table to match your query. Still, there are some pitfalls:
Problems caused by the data
Most likely there are some converted or non-printable characters in the input data (or database). For example, there could be a linefeed character or a peculiarly encoded symbol, or some characters such as < and > converted into HTML entities. As a result, the query contains <abc#abcs.com> will never match a text <abc#abcs.com>.
The problem is, this is only a guess, and nobody can tell you what the actual issue is, because it is your database, your input data and only you can find the issue.
I wrote an article that explains how to debug your PDO issues.
To debug a particular issue, you need
make sure the full error reporting is enabled for both PDO and PHP. It really helps, showing you occasional typographic errors, spelling errors and the such
scrutinize both the data in the database and the input to find the difference. bin2hex() function would help, revealing all non-printable and converted characters, in both database and the input.
Problems caused by the connection credentials
Another frequent issue is when you have several databases and connect to the wrong one that doesn't have the data requested. This issue is similar to this one, so just follow the same routine, only checking not the list of tables but the data rows.
Problems caused by character set/encoding
It's a rare case, but just to to be sure, follow the checklist from this great answer
An irrelevant but important note
On a side note, but very important nevertheless: your prepared statement is a cargo cult code that protects nothing. Here is how it must be:
$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();

Categories