生成我的表名,它包含以下列:(id, username, password)
- 没什么特别的:)
我的users.text有这个:
expert1001 01995056 expert1002 58381484 expert1003 03154500 expert1004 45885204 expert1005 24750107 expert1006 14096885 expert1007 81250562 expert1008 00910766 expert1009 99830724
我试图将这些生成的用户导出到我的数据库,但是我试图检查用户是否已经存在;如果是这样的话,我会转到下一个,但我收到了这个错误而且我没有看到它。
您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以获得在''附近使用的正确语法。在第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!";
?>
答案 0 :(得分:0)
我做了一些改变者,它现在似乎工作但我仍有一个问题是,在查询结束时我无法找到如何在提交查询之前修剪它
这是代码
<?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());
}
?>
这是查询结果
&#34;插入
users
(username
,password
)值(&#39; aspireupp7p&#39;, &#39; 97025407&#39;),(&#39; artre1r7pp&#39;,&#39; 40138199&#39;),(&#39; asuiozy4p&#39;, &#39; 44896449&#39;),(&#39; asuytrdiop&#39;,&#39; 36582797&#39;),&#34;
你看到最后我有这个,你能帮助我吗谢谢你
答案 1 :(得分:0)
我已经清理了一点代码..祝你好运!
<?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!");
$users = file('users.text'); # read file into array
if ($count = count($users)) # file is not empty
{
$milestone_query = "INSERT into `users`(`username`, `password`) values";
$milestone_query_count = strlen($milestone_query);
foreach ($users as $i => $user) {
$userInfo = explode(' ',trim($user));
$username = current($userInfo);
$password = next($userInfo);
$requete = "SELECT `username` FROM `users` WHERE `username` ='{$username}'";
if(!$result =boolval(mysql_fetch_assoc(mysql_query($requete)))){
var_dump($username." added!");
$milestone_query .= "('$username', '$password'),";
}
}
$milestone_query = trim($milestone_query, ",");
if(strlen($milestone_query) !== 50){
mysql_query($milestone_query) or die(mysql_error());
var_dump("finished!");
}else{
var_dump("No new users!");
}
}