Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

MySQL, insert not working

All I'm trying to do is ready from a text file of male first names and write each name to table called "namesmale", and my PHP goes through without any errors, and yet, no names are being written. What am I doing wrong??? Here is the code:

    $db = DB::getPDOConnection(); // gets a db connection
    
    $sql1 = " DROP TABLE IF EXISTS namesmale; "; // This works
    $sql2 = " CREATE TABLE namesmale ( " .
           " id INT(11) NOT NULL AUTO_INCREMENT, " .
           " firstname VARCHAR(30) NOT NULL, " .
           " PRIMARY KEY (id)) "; // This works, too
    
    $db->query($sql1); // This query works
    $db->query($sql2); // This query works
    
    $handle = fopen("../namesmale.txt", "r"); // The text file with list of male names
    while(!feof($handle)) {
        $line = fgets($handle); // Read a name
        $db->exec("INSERT INTO 'namesmale' ('firstname;) VALUES ('" . $line . "');"); // insert into database, THIS IS NOT WORKING!
    }
    fclose($handle); //close text file
    $db = null; // close db connection

Open in new window


Can anyone please tell me what I'm doing wrong? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil
elepil

ASKER

DOH! Didn't see that semicolon after firstname. Thanks!
You're welcome!