开发者

loop through mysql table and save to text file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_运维知识库 Closed 10 years ago.

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜