tunage Posted April 11, 2014 Share Posted April 11, 2014 I am trying to query tab delimited first and last names from a file against a table in mysql. I can see my data coming in from the file and test SQL works fine on the mysql command line, but I get zero results from my code. <?php require_once('auth.php'); $conn = new PDO("mysql:hostname=localhost;dbname=$dbname", $username, $pw); $file = fopen('nameList1.txt','r'); //change to whatever name it is $firstname = ''; $lastname = ''; $index = -1; $id = -1; $tab = ' '; //change this to the delimiter, of course 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 id FROM dbname WHERE FName = $firstname AND LName = $lastname"); $stmt->execute(); $result = $stmt->fetchAll(); var_dump($result); } fclose($file); ?> ****************************************************************** +-----------------------+--------------+------+-----+---------+----------------+ | id | mediumint(9) | NO | PRI | NULL | auto_increment | | StateVoterID | int(10) | YES | | NULL | | | CountyVoterID | int(10) | YES | | NULL | | | Title | char(30) | YES | | NULL | | | FName | char(30) | YES | | NULL | | | MName | char(30) | YES | | NULL | | | LName | char(30) | YES | | NULL | | | NameSuffix | char(30) | YES | | NULL | | | Birthdate | int(10) | YES | | NULL | | | Gender | char(30) | YES | | NULL | | | RegStNum | char(30) | YES | | NULL | | | RegStFrac | char(30) | YES | | NULL | | | RegStName | char(30) | YES | | NULL | | | RegStType | char(30) | YES | | NULL | | | RegUnitType | char(30) | YES | | NULL | | | RegStPreDirection | char(30) | YES | | NULL | | | RegStPostDirection | char(30) | YES | | NULL | | | RegUnitNum | int(10) | YES | | NULL | | | RegCity | char(30) | YES | | NULL | | | RegState | char(30) | YES | | NULL | | | RegZipCode | int(10) | YES | | NULL | | | CountyCode | int(10) | YES | | NULL | | | PrecinctCode | char(10) | YES | | NULL | | | PrecinctPart | char(30) | YES | | NULL | | | LegislativeDistrict | char(30) | YES | | NULL | | | CongressionalDistrict | char(30) | YES | | NULL | | | Mail1 | char(30) | YES | | NULL | | | Mail2 | char(30) | YES | | NULL | | | Mail3 | char(30) | YES | | NULL | | | Mail4 | char(30) | YES | | NULL | | | MailCity | char(30) | YES | | NULL | | | MailZip | char(30) | YES | | NULL | | | MailCountry | char(30) | YES | | NULL | | | Registrationdate | char(30) | YES | | NULL | | | AbsenteeType | char(30) | YES | | NULL | | | LastVoted | char(30) | YES | | NULL | | | StatusCode | char(30) | YES | | NULL | | +-----------------------+--------------+------+-----+---------+----------------+ 37 rows in set (0.00 sec) ********************************** test SQL *************************************************** mysql> SELECT * FROM list WHERE FName='TARA' AND LName='AABELLERA'; +----+--------------+---------------+-------+-------+-------+-----------+------------+-----------+--------+----------+-----------+-----------+-----------+-------------+-------------------+--------------------+------------+------------+----------+------------+------------+--------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+------------+------------+ | 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 | AABELLERA | | 2 | F | 5804 | | 141ST | PL | | | NE | 0 | MARYSVILLE | WA | 98271 | 0 | 101231 | 0 | 39 | 2 | | | | | | | | 05/05/2004 | P | 11/08/2011 | A | +----+--------------+---------------+-------+-------+-------+-----------+------------+-----------+--------+----------+-----------+-----------+-----------+-------------+-------------------+--------------------+------------+------------+----------+------------+------------+--------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+------------+------------+ 1 row in set (27.22 sec) *********************** var_dump() ************************************************* array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) Quote Link to comment Share on other sites More sharing options...
tunage Posted April 11, 2014 Author Share Posted April 11, 2014 code and error update http://bpaste.net/show/200663/ Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in /var/www/localhost/htdocs/votebd/query2.php on line 15array(0) { } Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted April 11, 2014 Share Posted April 11, 2014 You need to wrap $firstname and $lastname in your query in single quotes. Try, $stmt = $conn->prepare("SELECT id FROM dbname WHERE FName = '$firstname' AND LName = '$lastname'"); Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 11, 2014 Share Posted April 11, 2014 (edited) in your revised code, you are not binding (or supplying an array in the execute() method call) any variables or values for the two place-holders in the prepared query. also, you should (almost) never run queries inside of loops, even prepared queries only save a minimal amount of time in a loop because you must sill do a round-tirp to the database server to supply the values for the query, and if you were running a prepared query inside a loop, the only query statement logic inside the loop would be the call to the execute() method. by putting the prepare() statement inside the loop, you are re-preparing the query each pass through the loop and the posted code is taking twice as long to run as a non-prepared query would take. since it's not clear what you are attempting to use the result from the query to do, it's not possible to advise on the best method to use to avoid running a query inside of a loop. however, if you are going to insert new data or insert new data/update existing data, you would typically define a unique key and either just use a query like INSERT IGNORE ... or an INSERT ... ON DUPLICATE KEY UPDATE ..., rather than first trying to select data to decide what to do in the code to handle the new/updated values. also, your try/catch block won't work unless you set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION Edited April 11, 2014 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.