1 week ago by Nicklas Raab

Relates to the lesson PHP Authentication System


I've been using the login system (https://youtu.be/c_hNNAdyfQk) and it works great in my opinion. Even updated the password handling :) I am though having trouble when I need to do a queries within a foreach loop.

Let's say I do this:

      $param = array(1);
      $stmt = "SELECT * FROM floors WHERE fst_id_fst = ?";
      $floors = DB::getInstance()->query($stmt, $param);

I then loop through all floors. Within that foreach loop I want to take an ID and do another query and print those results. The problem is that when the query inside the loop is done, the original query is gone. I'm looping over it like so:

foreach($floors->results() as floor) {


I've tried using different variable-names for stmt and floors. I'm guessing that I change the DB class private variables. But that leads me to a road block where I either need to establish multiple connections to the database in some other way or store all of the results from floors (say $floors = $floors->results() ). Either way seems bad and ineffective and I can't use the methods for the object since it's now an array...

Great need of help here :) I'm sure I've just missed something!

Oh, and since I'm new to all of this - is this course outdated in some way that I need to know? Kind regards and big thanks!

1 week ago by Kristian Kjornes

You should do some joins in the first query so that you wont have to do a foreach loop.

1 week ago by Lee Weaver

Thye first thing your doing wrong ifs you cant do a foreach statement without the results the DB needs to know if here results

Sign in or create an account to reply to this topic.