微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP / MYSQL:迭代数据库中的每条记录

我是整个PHP / mysql的新手.我有一周的服务器日志(大约300,000项),我需要做一些分析.我打算将它们全部读入MysqL数据库,然后用PHP分析它们.

我不确定的是如何迭代它们.使用java读取文件我会做这样的事情:

Scanner s = new Scanner(myfile);
while(s.hasNext()){
    String line = s.nextLine();
    ~~ Do something with this record. 
}

如何使用PHP迭代MysqL数据库中的所有记录?我认为像这样的东西需要愚蠢的记忆.

    $query = "SELECT * FROM mytable";
    $result = MysqL_query($query);
    $rows = MysqL_num_rows($result);
    for($j = 0; $j < $rows; ++$j){
            $curIndex   = MysqL_result($result,$j,"index");
            $curURL     = MysqL_result($result,$j,"something");
            ~~ Do something with this record
    }

所以我在select语句中添加一个限制,我重复一遍,直到所有记录都循环完毕.有更标准的方法吗?有没有内置的可以做到这一点?

while($startIndex < $numberOfRows){

    $query = "SELECT * FROM mytable ORDERBY mytable.index LIMIT $startIndex,$endindex";
    $result = MysqL_query($query);
    $rows = MysqL_num_rows($result);
    for($j = 0; $j < $rows; ++$j){
            $curIndex   = MysqL_result($result,$j,"index");
            $curURL     = MysqL_result($result,$j,"something");
            ~~ Do something with this record
    }
    $startIndex = $endindex + 1;
    $endindex = $endindes + 10;
}

解决方法:

看这里:

http://www.tizag.com/mysqlTutorial/

http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php

<?PHP
// Make a MysqL Connection
$query = "SELECT * FROM example"; 

$result = MysqL_query($query) or die(MysqL_error());


while($row = MysqL_fetch_array($result)){
    echo $row['name']. " - ". $row['age'];
    echo "<br />";
}
?>

根据您对结果行的需要,您可以使用不同的循环样式,无论是“while”,“for each”还是“for x to x”.大多数情况下,简单的“while”迭代将是很好的,并且是有效的.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐