Posts

Showing posts with the label sorting

Things you should not do in PHP (update: references)

Here is a list of things you should not do in PHP. Most of the stuff is pretty obvious, but over the years I've seen a lot of them. In most cases, these problems remain hidden until data grows above 10000 entries. So on a development system, things are always fast and there are no problems with memory limits :-) Suppose we have a table with 100k entries: $db->query('create table stats (c1 int(11) primary key, c2 varchar(255))'); $db->query('begin'); for ($i=0; $i<100000; $i++) { $db->query('insert into stats values ('.$i.','.($i*2).')'); } $db->query('commit'); Populate a big array instead of streaming results: $result = $db->query('select * from stats'); $array = $result->fetch_all(); // 35M // or while ($row = $result->fetch_assoc()) $array[] = $row; // 35M // or while ($row = $result->fetch_array()) $array[] = $row; // 44.5M // process $array ... // instead of: while ($row = $result->fetch...