gpt4 book ai didi

php - 将用户从文本文件导入到 MySQL

转载 作者:行者123 更新时间:2023-11-30 00:26:01 26 4
gpt4 key购买 nike

我的表名称已生成,它包含以下几列:(id、用户名、密码) - 没什么花哨的:)

我的 users.text 有这个:

expert1001 01995056
expert1002 58381484
expert1003 03154500
expert1004 45885204
expert1005 24750107
expert1006 14096885
expert1007 81250562
expert1008 00910766
expert1009 99830724

我正在尝试将这些生成的用户导出到我的数据库,但我正在尝试检查用户是否已经存在;如果是这样,我将继续下一个,但我收到此错误,但我没有看到它。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

(PHP 版本 5.3.5)

<?php
$host = "localhost";
$user = "root";
$pass = "1234";
$db = "users";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
$file = file('users.text'); # read file into array
$count = count($file);
if($count > 0) # file is not empty
{
$milestone_query ="INSERT into `generated`(`username`, `password`) values";
$i = 1;
foreach($file as $row)
{
$milestone = explode(' ',$row);
$requete ="SELECT * FROM `lines` WHERE `username`='$milestone[0]'";
$queryset=mysql_query($requete);
while($row2 = mysql_fetch_assoc($queryset)){
$bood = "".$row2['username']."";
$us=$milestone[0];
if ($us == $bood )
{
$i++;
} else {
$milestone_query .= "('$milestone[0]', '$milestone[1]')";
$milestone_query .= $i < $count ? ',':'';
$i++;
}
}
}
if ($milestone_query != NULL ){

mysql_query($milestone_query) or die(mysql_error());

}else{
die();
}
}
echo "Done!";
?>

最佳答案

我做了一些更改,它现在似乎可以工作,但我仍然有一个问题是,在查询结束时我无法找到如何在提交查询之前修剪它

这是代码

<?php
$host = "localhost";
$user = "root";
$pass = "123456";
$db = "generated";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
$file = file('users.text'); # read file into array
$count= count($file);
if($count > 0) # file is not empty
{
$milestone_query ="INSERT into `users`(`username`, `password`) values";
$i = 1;
foreach($file as $row)
{
$row = str_replace("\n", '', $row);
$milestone = explode(' ',$row);
$us= $milestone[0];
$requete ="SELECT `username` FROM `users` WHERE `username`='$us'";
$queryset=mysql_query($requete);
$row2 = mysql_fetch_assoc($queryset);
$bood = $row2['username'];
if ($us == $bood)
{
$i++;

} else{
$milestone_query .= "('$milestone[0]', '$milestone[1]')";
$milestone_query .= $i < $count ? ',':'';

}

}
mysql_query($milestone_query) or die(mysql_error());
}
?>

这里是查询结果

"INSERT into users(username, password) values('aspireupp7p', '97025407'),('artre1r7pp', '40138199'),('asuiozy4p', '44896449'),('asuytrdiop', '36582797'),"

你看到最后我有这个你们能帮我吗谢谢

关于php - 将用户从文本文件导入到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22879456/

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