gpt4 book ai didi

php 从文件读取但我在 mysql 中输入空白

转载 作者:行者123 更新时间:2023-11-29 14:10:16 25 4
gpt4 key购买 nike

我正在尝试读取一个文件,然后我正在尝试插入到 mysql 表中我尝试了以下操作,得到了 te 文本,并显示了文件中的文本,但是当我尝试插入数据库时​​,该行已创建,但里面什么也没有有什么帮助吗?

这是我的代码

<?php

$file_handle = fopen("text.htm", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line;
// make the DSN
$dsn = 'mysql:host=localhost;dbname=text;';
$user = 'text';
$password = 'text';
$name=$line;
}
try
{
$dbh = new PDO($dsn, $user, $password);
// set the error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$sql = 'INSERT INTO text (db_text)
VALUES (:name)';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':name', $name);
$stmt->execute();
}
catch (PDOException $e)
{
echo 'PDO Exception Caught. ';
echo 'Error with the database: <br />';
echo 'SQL Query: ', $sql;
echo 'Error: ' . $e->getMessage();
}
fclose($file_handle);

?>

最佳答案

我觉得你应该改变

$line = fgets($file_handle);

$line .= fgets($file_handle);

在 while 循环内。

<小时/>

此外,在 while 循环之前的某处使用 $line = ""; 实例化 $line

fgets 上次可能会返回 false(或者空行或其他内容)。

关于php 从文件读取但我在 mysql 中输入空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com