2012/12/17 dealTek <dealtek@xxxxxxxxx> > > On Dec 16, 2012, at 10:08 AM, Sebastian Krebs <krebs.seb@xxxxxxxxx> wrote: > > > 2012/12/16 dealTek <dealtek@xxxxxxxxx> > > > >> Hi all, > >> > >> Noobie starting to learn oop for databases from here: > >> > >> > >> > https://github.com/JeffreyWay/PHP-MySQL-Database-Class/blob/master/MysqlDb.php > >> > >> I've got lots working but have a few issues: > >> > >> 1 - after an insert I'd like to get the id of the new record and I'm not > >> sure how to get that... > >> > >> mysql_insert_id (depricated?) or mysqli_insert_id() (I am using mySql > 5.3) > >> > >> not sure where to add this... (most likely in MysqlDb.php but I don't > know > >> where or how...) > >> > > > > Instead of "true" let insert() return the id. > > > > And while looking at your code: > > - You wrote in your DocBlocks, that the methods returns a boolean 0 or 1. > > Beside that this is wrong (0 or 1 are integers) you return either 'true' > or > > nothing. You should return 'false' as well. > > - Returning a boolean to indicate the success of a method only makes > sense, > > when "not successful" is a valid case, but I guess when 'delete()' fail > it > > not be treatened as "normal". You should throw an Exception instead. This > > also includes: It's not required, that a method returns something in > every > > case. If "delete()" for example doesn't have to tell something, it > > shouldn't. > > > > > >> > >> http://de.php.net/manual/en/function.mysql-insert-id.php > >> > >> 2 - how does one do aggrigate select queries like "SELECT SUM(price) > FROM > >> mytable" ... what I tried seemed to fail... > >> > > > > Nothing "seems to fail" ;) Either it fails, or not (or it just doesn't > > behave, like expected, what I see as "fail" too). So what happens? > > > > Hi Sebastian, > > Of course you're right.... well it does fail here... > > when I try a page with this... > > $results = $db->query('SELECT SUM(price) FROM tbl_1218'); > > the error is.... > Fatal error: Problem preparing query in /Users/me/Sites/db/test/sqldb.php > on line 281 > > > /** > * Method attempts to prepare the SQL query > * and throws an error if there was a problem. > */ > protected function _prepareQuery() > { > //echo $this->_query; rev this is now fixed with this update > if (!$stmt = $this->_mysql->prepare($this->_query)) { > trigger_error("Problem preparing query", E_USER_ERROR); <<<<<--- > this is line 281 > } > return $stmt; > } > > Use mysqli_error() to get the _real_ error. By the way it's better to use exceptions http://php.net/mysqli.error.php http://php.net/mysqli.error-list http://php.net/mysqli.error > > > > > > >> > >> > >> And if anyone can point to some good OOP training URL's I'd appreciate > it. > >> > >> Thanks in advance for any assistance... > >> > >> > >> > >> > >> > >> > >> > >> -- > >> Thanks, > >> Dave - DealTek > >> dealtek@xxxxxxxxx > >> [db-12] > >> > >> > >> -- > >> PHP General Mailing List (http://www.php.net/) > >> To unsubscribe, visit: http://www.php.net/unsub.php > >> > >> > > > > > > -- > > github.com/KingCrunch > > > -- > Thanks, > Dave - DealTek > dealtek@xxxxxxxxx > [db-12] > > -- github.com/KingCrunch