Re: [PHP] Noobie starting to learn OOP for databases needs help
On Sun, Dec 16, 2012 at 9:38 AM, dealTek wrote: > Noobie starting to learn oop for databases from here: Cool. :) > mysql_insert_id (depricated?) or mysqli_insert_id() (I am using mySql 5.3) The mysqli version would be preferred, since you're using mysqli in that class. > 2 - how does one do aggrigate select queries like "SELECT SUM(price) FROM > mytable" ... what I tried seemed to fail... Problem there is that the query method forces everything through prepare, and you don't have a preparable statement. I'd write another method for such single values where you won't sent it through prepare/execute/bind, and just suck in the value and return it. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Noobie starting to learn OOP for databases needs help
2012/12/17 dealTek > > On Dec 16, 2012, at 10:08 AM, Sebastian Krebs wrote: > > > 2012/12/16 dealTek > > > >> 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 > >> deal...@gmail.com > >> [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 > deal...@gmail.com > [db-12] > > -- github.com/KingCrunch
Re: [PHP] Noobie starting to learn OOP for databases needs help
On Sun, Dec 16, 2012 at 2:08 PM, Matijn Woudt wrote: > On Sun, Dec 16, 2012 at 4:38 PM, dealTek wrote: > > > 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...) > > > > http://de.php.net/manual/en/function.mysql-insert-id.php > > > > try SELECT LAST_INSERT_ID(); > it should give you the id of the last inserted row (on a connection basis). > > > > > > > > > 2 - how does one do aggrigate select queries like "SELECT SUM(price) FROM > > mytable" ... what I tried seemed to fail... > > > > > Please show is exact query that failes, and give the error it returns. > > > > > > And if anyone can point to some good OOP training URL's I'd appreciate > it. > > > > Thanks in advance for any assistance... > > > > > Note sure what OOP has to do with MySQL, but I'd say google for PHP OOP and > there's plenty of good stuff available. > > - Matijn > I'd do select sum(price) as price that way the column name returned is "price" and not a derived column name
Re: [PHP] Noobie starting to learn OOP for databases needs help
On Dec 16, 2012, at 10:08 AM, Sebastian Krebs wrote: > 2012/12/16 dealTek > >> 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; } > >> >> >> 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 >> deal...@gmail.com >> [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 deal...@gmail.com [db-12]
Re: [PHP] Noobie starting to learn OOP for databases needs help
On Sun, Dec 16, 2012 at 4:38 PM, dealTek wrote: > 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...) > > http://de.php.net/manual/en/function.mysql-insert-id.php try SELECT LAST_INSERT_ID(); it should give you the id of the last inserted row (on a connection basis). > > > 2 - how does one do aggrigate select queries like "SELECT SUM(price) FROM > mytable" ... what I tried seemed to fail... > > Please show is exact query that failes, and give the error it returns. > > And if anyone can point to some good OOP training URL's I'd appreciate it. > > Thanks in advance for any assistance... > > Note sure what OOP has to do with MySQL, but I'd say google for PHP OOP and there's plenty of good stuff available. - Matijn
Re: [PHP] Noobie starting to learn OOP for databases needs help
2012/12/16 dealTek > 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? > > > 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 > deal...@gmail.com > [db-12] > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- github.com/KingCrunch