开发者

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

开发者 https://www.devze.com 2023-03-07 01:38 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
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

精彩评论

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