The following warnings occurred: | ||||||||||||||||||||||||
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
|
Retrieving details from a Database to table - Printable Version +- UserSpice (https://userspice.com/forums) +-- Forum: Miscellaneous (https://userspice.com/forums/forumdisplay.php?fid=28) +--- Forum: Documentation (https://userspice.com/forums/forumdisplay.php?fid=30) +--- Thread: Retrieving details from a Database to table (/showthread.php?tid=631) |
Retrieving details from a Database to table - faguss - 07-11-2017 Code: $customers = $db->query("SELECT * FROM customers")->results(); Retrieving details from a Database to table - sire - 07-11-2017 wow thank you Retrieving details from a Database to table - firestorm - 07-11-2017 a ha didn't realise you could do: Code: $customers = $db->query("SELECT * FROM customers")->results(); i normally: <pre> Code: $customerQ = $db->("SELECT * FROM customers"); foreach on $customers. learnt something new today Retrieving details from a Database to table - faguss - 07-11-2017 Code: $customerQ = $db->query("SELECT * FROM customers") Code: $customers = $customerQ->results(); This doesn't make sense to me because $customerQ is not a copy of the object. Just use $db variable and check for error first. Otherwise $customers are going to be results from the previous query. Code: if (!$db->query("SELECT * FROM customers")->error()) Code: $customers = $db->results(); In my version results are always cleared. ==== Also I'll hijack this thread to mention inconsistent return values. It's kinda weird to me that you can do this: Code: $array = $db->query(...)->results(); but not this: Code: $array = $db->get(...)->results(); I actually made a modification where it would always return object. However, I reverted it because there was no benefit for me. My queries have gotten complicated and I just use query() for getting data and rarely other functions. EDIT: On the other hand returning bool forces you to check error which is a good practice. Retrieving details from a Database to table - mudmin - 07-11-2017 I usually do it the way you're doing it because I wind up going back and doing my query, and then a lot of times I go and do a count and put my results statement in an if statement that decides what to do if for some reason results are less than 1. There are plenty of times I think there will ALWAYS be results and then I break something But yeah, you can string a lot of the methods together. I do it a lot for count if the only thing I care about is a count, too. Retrieving details from a Database to table - mudmin - 07-11-2017 @faguss, I'm definitely interested in checking out your mods, I will say that there are some use cases for doing it the other way.... Assigning the query to a variable lets me get both a count and results or first. A lot of times, I don't want results() because I only want the first/only result and in that situation it simplifies the code to do first() because I don't have to deal with the full object by either doing a foreach or $results[0]->mycolumn; Retrieving details from a Database to table - faguss - 07-13-2017 >There are plenty of times I think there will ALWAYS be results and then I break something I recommend to write: Code: if (!$db->error() && $db->count()>0) or to have an empty result array in case of error so that foreach will not happen: Code: $array = !$db->error() ? $db->results(true) : []; Code: foreach ($array as $item) {}; >Assigning the query to a variable lets me get both a count and results or first You can just do this: Code: $first = $db->query($sql)->first(); Code: $count = $db->count(); Retrieving details from a Database to table - Katronix - 08-27-2017 I'm sure there are / will be many people who learned to talk have PHP talk to MySQL the non-PDO way, it would be nice if we could have something like this thread in the documentation. I was trying to figure out for my current project how to process many records and unless I really didn't understand it the Documentation on the DB class doesn't explain this info at all. Just a suggestion Retrieving details from a Database to table - mudmin - 08-27-2017 Yeah. We're doing an actual documentation knowledgebase alongside with the dev of 4.3. Things were moving so fast that it's been hard to keep up. Right now, this video is probably the single best way to learn our db class. https://www.youtube.com/watch?v=rb0kD5tCENM Retrieving details from a Database to table - Brandin - 08-27-2017 Just an FYI about this video - this is the only reason I know how to code in my US project using PDO! It's very informative. This video and the messaging build are great! |