MySQL Handy Tricks
Let’s get the ball rolling wit this handy one about inserting csv or other delimited data to mysql dbs using INFILEs. This looks like a basic php version too.
When getting the annoying error 1075 “Incorrect table definition; There can only be one auto column and it must be defined as a key” when trying to add a new primary key, this page has an awesome solution.
Using sprintf() to zerofill variables, handy like so:
$zeroball1 = $this->escape($result->ball1);
$zeroball2 = sprintf("%02d",$zeroball1);
echo $zeroball2;
Zend Framework and MySQL
For counting across rows, first check out the excellent official mysql page on row counting. Then to apply it to Zend framework, this is probably the best starting place for MySql select queries in Zend FW, and I check it regularly. Basically, to run a mysql query like SELECT `results`.`bonus`, COUNT(*) AS `counter` FROM `results` WHERE (draw = 0) GROUP BY `bonus`; you need the following in your model. Note the ‘counter’ => ‘COUNT(*)’ syntax…
$query = $db->select()
->where("draw = $draw_val")
->from('results',array('bonus','counter' => 'COUNT(*)'))
->group('bonus');
For further and more detailed quality examples of Zend and MySql, this is good. Check out lars’ comment here on counting across multiple tables.
