我是整个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”迭代将是很好的,并且是有效的.