loop through mysql table and save to text file [closed]
I have a table called user_details
this table has got morethan 100k user details
its index is username
. I want loop through index(username) and save to a text file enclose with <li>
tag (ex: <li>mathew</li>
) by only 10k user names per each file.
the content of text file must be:
<li>mathew</li>
<li>john</li>
<li>ethan</li>
<li>kenan</li>
<li>brandon</li>
// http://www.php.net/manual/en/function.mysql-fetch-array.php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT username, name FROM user_details");
$nr = 0;
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$nr++;
if ($nr >= 10000) {
$nr = 0;
$i++;
}
$data = '<li>'.$row[0].'</li>'."\n";
file_put_contents(dirname(__FILE__).'/usernames-'.$i.'.txt', $data, FILE_APPEND);
}
精彩评论