2012-01-05 70 views
0

我能夠連接到MySQL數據庫並創建一個表,但沒有數據,並想知道我失蹤或做錯了什麼?我在下面發佈我的功能代碼。我的回聲計數被確認,我也檢查了我的Poems.csv以確保它們不是空白的。php mysql連接並創建表沒有錯誤,但沒有數據

function writeQuotestoDb() { 
    $input_csv = fopen("Poems.csv", "r"); 
    $author_names = array(); 
    $poem_names = array(); 
    $poem_urls = array(); 

    while (($columns = fgetcsv($input_csv, 1000, ",")) !== FALSE) { 
    array_push($author_names, $columns[0]); 
    array_push($poem_names, $columns[1]); 
    array_push($poem_urls, $columns[2]); 
    } 
    fclose($input_csv); 

    $dbh = mysql_connect('localhost', 'root', ''); 
    mysql_select_db('test', $dbh); 
    mysql_query("CREATE TABLE IF NOT EXISTS `poem2` (
      `id` INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY , 
      `author_name` varchar(128) DEFAULT NULL, 
       `poem_name` varchar(128) DEFAULT NULL, 
       `poem_text` text, 
       `poem_url` varchar(1024) DEFAULT NULL 
      ) ENGINE = MYISAM"); 
    mysql_query("TRUNCATE `poem2`");  
    for ($i=0; $i<count($author_names); $i++) { 
     $poems[$i] = substr($poems[$i],7,(strlen($poems[$i])-14)); 
     $poems[$i] = str_replace('"','',$poems[$i]); 
     $query = 'INSERT INTO `poem2` VALUES (NULL,"' . $author_names[$i] . '", "' . $poem_names[$i].'","' . $poem_urls[$i] .'")'; 
     mysql_query($query); 

    } 
    echo count($author_names)." rows....successfully db updated"; 
} 
+0

在最後一個mysql_query($ query)之後;添加var_dump(mysql_error());並看看有什麼錯誤。 – 2012-01-05 04:05:03

+0

這是結果 字符串(47)「列計數與第1行的值計數不匹配」字符串(47)「列計數與第1行的值計數不匹配」 15038 rows .... successfully db updated – merrill 2012-01-05 04:09:59

+0

錯誤本身說明查詢有什麼問題。您缺少插入查詢中的'poem_text'。 – Virendra 2012-01-05 04:37:56

回答

1

您從插入查詢中缺少poem_text。您可以使用下面的更新的代碼:

function writeQuotestoDb() { 
    $input_csv = fopen("Poems.csv", "r"); 
    $author_names = array(); 
    $poem_names = array(); 
    $poem_urls = array(); 

    while (($columns = fgetcsv($input_csv, 1000, ",")) !== FALSE) { 
    array_push($author_names, $columns[0]); 
    array_push($poem_names, $columns[1]); 
    array_push($poem_urls, $columns[2]); 
    array_push($poem_text, $columns[3]); // If columns[3] does not exists you can just use array_push($poem_text, ''); 
    } 
    fclose($input_csv); 

    $dbh = mysql_connect('localhost', 'root', ''); 
    mysql_select_db('test', $dbh); 

    mysql_query("CREATE TABLE IF NOT EXISTS `poem2` (
      `id` INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY , 
      `author_name` varchar(128) DEFAULT NULL, 
       `poem_name` varchar(128) DEFAULT NULL, 
       `poem_text` text, 
       `poem_url` varchar(1024) DEFAULT NULL 
      ) ENGINE = MYISAM"); 
    mysql_query("TRUNCATE `poem2`");  
    for ($i=0; $i<count($author_names); $i++) { 
     $poems[$i] = substr($poems[$i],7,(strlen($poems[$i])-14)); 
     $poems[$i] = str_replace('"','',$poems[$i]); 
     $query = 'INSERT INTO `poem2`(`author_name`, `poem_name`, `poem_text`, `poem_url`) VALUES ("' . $author_names[$i] . '", "' . $poem_names[$i] . '", "' . $poem_text[$i] . '","' . $poem_urls[$i] .'")'; 
     mysql_query($query); 

    } 
    echo count($author_names)." rows....successfully db updated"; 
} 

如果你不想插入poem_text就用下面的插入語句,而不是原來的插入查詢。

$query = 'INSERT INTO `poem2`(`author_name`, `poem_name`, `poem_url`) VALUES ("' . $author_names[$i] . '", "' . $poem_names[$i] . '","' . $poem_urls[$i] .'")'; 
+0

中刮取的數據完美!謝謝! – merrill 2012-01-05 10:26:39

相關問題