-1

I get array(0){} where I should be returning rows;

<?php
require_once('auth.php');
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $pw);
$file = fopen('nameList1.txt','r');
$firstname = ''; $lastname = ''; $index = -1; $id = -1;
$tab = '        ';
try{
while ($line = fgets($file)) {
        $index = strrpos($line, $tab);
        $firstname = substr($line, 0, $index);
        //var_dump($firstname);
        $lastname = substr($line, $index + strlen($tab));
        //var_dump($lastname);
        $stmt = $conn->prepare("SELECT * FROM dbname WHERE FName = :firstname AND LName = :lastname");
        $stmt->bindvalue(':firstname', $firstname);
        $stmt->bindvalue(':lastname', $lastname);
        $stmt->execute();
        $result = $stmt->fetchAll();
        var_dump($result);
        }
}
catch ( Exception $e )
{
  echo "Exception at $i";
}
fclose($file);
?>

********* working SQL *************

mysql> SELECT * FROM list WHERE FName='TARA' AND LName='AABA';
+----+--------------+---------------+-------+-------+-------+-----------+------------+-----------+--------+----------+-----------+-----------+-----------+-------------+-------------------+--------------------+------------+------------+----------+------------+------------+--------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+------------+------------+
| id | StateVoterID | CountyVoterID | Title | FName | MName | LName     | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStName | RegStType | RegUnitType | RegStPreDirection | RegStPostDirection | RegUnitNum | RegCity    | RegState | RegZipCode | CountyCode | PrecinctCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted  | StatusCode |
+----+--------------+---------------+-------+-------+-------+-----------+------------+-----------+--------+----------+-----------+-----------+-----------+-------------+-------------------+--------------------+------------+------------+----------+------------+------------+--------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+------------+------------+
|  9 |            0 |        458198 |       | TARA  | L     | AABRA |            |         2 | F      | 5804     |           | 1ST     | PL        |             |                   | NE                 |          0 | MARYSVILLE | WA       |      98271 |          0 | 101231       | 0            | 39                  | 2                     |       |       |       |       |          |         |             | 05/05/2004       | P            | 11/08/2011 | A          |

*********** var_dump($result) **************

array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } ....
brad
  • 870
  • 2
  • 13
  • 38
  • tried running this function after executing the statement? http://www.php.net/manual/en/pdo.errorinfo.php and you can make pdo raise exception instead of error with this http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo – Jeffrey04 Apr 11 '14 at 04:09
  • @Jeffrey04 I was throwing errors before about invalid parameters, but the bindvalue() fixed the error but not the issue. That's what the try/catch is for and #5 on your link says to do it that way. Not sure what to do now. – brad Apr 11 '14 at 04:15
  • and, is your strpos giving you the right answer all the time? if you are sure all your tab characters are consistent, try doing an list($firstname, $lastname) = explode($tab, $line), and then your F/Lname would be $firstname, $lastname respectively – Jeffrey04 Apr 11 '14 at 04:16
  • you are really needing this after instantiating your pdo: $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); – Jeffrey04 Apr 11 '14 at 04:19
  • @Jeffrey04 with your code edits, I get wsod http://bpaste.net/show/200693/ – brad Apr 11 '14 at 04:37
  • not really white screen of death, more like your input is not right (hence query not returning result), check your input file for the delimiter format, are you sure $tab = ' ' is correct? – Jeffrey04 Apr 11 '14 at 06:09

1 Answers1

0

Empty array returned by fetchAll means there was no data found.

This is a fact. While "working SQL" is working somewhere else.

So - you have to check your input data, database, environment, typos and such.

Sincerely,

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345