0

Hi I just want to know if is it possible to have different queries with DB::getInstance() value in query at the same time on the same page? and is it good practice to do? if Not can you show me the best practice?

I have update page (update.php) on my update page I have querying the part where you would get the id in the database.

this works fine.

$id = $_GET['id'];   
$agent = DB::getInstance()->get('agent', array('id', '=', $id));

Below of my update.php page is a drop down field where it will querying some part of my table data. for instance it is a director table where this user is belong.

But it does not work.

            $directors = $dbh->query("SELECT * FROM directors");


             echo "<select name='director' id='director'>
                    <option value=''>".$agent->results()[0]->agent_name."</option>";

             foreach($directors->results() as $director){
                echo "<option value='$director->name'>".$director->name."</option>";
             }

             echo "</select>";

Thank you

Jayson Lacson
  • 63
  • 1
  • 2
  • 11

1 Answers1

-4

Although it's impossible to answer your question directly, there are some directions

  1. Don't use getinstance() method. it just makes no sense nowadays.
  2. DON'T use get() method. It never had any sense at all.

If you want a singleton approach, then create a wrapper that already being a PDO instance. You may find an example here.

And you will be able to run any number of queries of any type:

$sql   = 'SELECT * FROM agent WHERE id = ?';
$agent = DB::prepare($sql)->execute([$_GET['id']])->fetch();

$directors = DB::query("SELECT * FROM directors")->fetchAll();

It seems that you also mistaken in using query results

echo "<select name='director' id='director'>
    <option value=''>$agent->agent_name</option>";

foreach($directors as $director){
   echo "<option value='$director->name'>$director->name</option>";
}
echo "</select>";
Your Common Sense
  • 152,517
  • 33
  • 193
  • 313