I have some PHP code I need to port to node.js / Javascript and I dont know how to do this properly: fetching only one row at a time.
I have a query that results in a MASSIVE dataset; I only need a few of the results; but I dont know how many.
In PHP I just execute the query and then fetch row per row until the condition is met. php pseudo code:
$sql="select price, totvol from depth where.. order by price desc";
$result = mysql_query($sql) or die(mysql_error()); //massive dataset
while ($row=mysql_fetch_assoc($result) and some condition) {
do stuff until condition is met
}
I dont see how I can this in node-mysql. I could add a LIMIT 1 to the sql query and make a new query each time, but then how do I get the next row in the ordered list? Any tips would be much appreciated.
If it helps, this is the actual code:
$sql='';
if ($type=="bid") $sql="select price, totvol from depth where price>=$price order by price asc";
elseif ($type=="ask") $sql="select price, totvol from depth where price<=$price order by price desc";
else echo "unknown type\n";
if ($sql!='') {
$result = mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_assoc($result) and $amount_int>0) {
$newamount=max($row["totvol"]-$amount_int,0);
$amount_int-=$row["totvol"];
$sql2="update depth set totvol=$newamount where price=".$row["price"];
$result2 = mysql_query($sql) or ie(mysql_error());
}
}