The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieving details from a Database to table
#11
Code:
$customers = $db->query("SELECT * FROM customers")->results();
  Reply
#12
wow thank you Smile
  Reply
#13
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");
$customers = $customerQ->results();
</pre>


foreach on $customers. learnt something new today Smile
  Reply
#14
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.
  Reply
#15
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 Smile 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.
  Reply
#16
@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;
  Reply
#17
>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();
  Reply
#18
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 Smile
  Reply
#19
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
  Reply
#20
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!
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)