This has had me scratching my head for a while...
my $crid = $crm->db->selectrow_array("SELECT idPerson FROM Person WHER
+E email = ? OR altEmail = ?", undef, $data{'email'}, $data{'email'});
print "ERROR: " . $crm->db->errstr if $crm->db->err;
print "#$data{'email'}# - $crid";
The print statements are only there for debugging. The # is around the email to check no extraneous spaces had crept in which they haven't. $crm is my CRM module and the db method returns the database handle.
The output I get is #me@example.com# - 0
idPerson is an auto increment INT which starts at 1. There is no zero in the database!
If I manually query the database I get this:
SELECT idPerson FROM Person WHERE email = 'me@examle.com' OR altEmail
+= 'me@example.com'
90
102
Yet my code returns 0.
Things I have tried:
- Pulling $crm->db into a separate variable first - no change
- Replacing the placeholders with the variables WHERE email = '$data{'email'}' - no change.
- Hardcoding the email WHERE email = 'me\@example.com' - that works!
- Pulling the email into a separate variable my $email = $data{'email'} then putting that into the query WHERE email = '$email' - doesn't work
- Forcing array context my ($crid) = $crm->db->selectrow_array - no change
I have found a workaround:
my $query = $crm->db->prepare("SELECT idPerson FROM Person WHERE email
+ = ? OR altEmail = ?");
$query->execute($data{'email'}, $data{'email'});
my $crid = $query->fetchrow_array;
print "ERROR: " . $crm->db->errstr if $crm->db->err;
But I have
selectrow_array working like this in other places. I don't understand what is going on here!
The DBI documentation says that selectrow_array is the equivalent of prepare, execute and fetchrow_array. It also says that selectrow_array returns undef if there is an error. Here it is returning zero which is not in the database!
Can you help me understand what is going on here?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.