php mysql 连接并创建表,没有错误,但没有数据

标签 php mysql sql database scrape

我能够连接到 mysql 数据库并创建一个表,但没有数据,想知道我缺少什么或做错了什么?我在下面发布了我的功能代码。我的 echo 计数已确认,我还检查了我的 Poems.csv 以确保它们不为空。

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";
 }

最佳答案

您的插入查询中缺少诗歌文本。您可以使用下面更新的代码:

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] .'")';

关于php mysql 连接并创建表,没有错误,但没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8737482/

相关文章:

mysql - 如果已经存在时间,我如何验证时间?

java - 使用 PreparedStatement 执行递增和递减

mysql - SQL意外返回

mysql - 最好使用两列或 DATETIME

sql - Oracle 每月总计以及列和行总计

php - 上传文件时出现 HTTP 500 内部服务器错误

php - Magento 管理面板登录后重定向到 404

php - mysql_data_seek 在 ff 和 chrome 中出错,但在 ie 中有效

php - 如何在 1-n 之间选择 RAND()

php - 使用 $_GET 的解析和安全性