jeudi 20 avril 2017

PHP PDO MYSQL -> Query using PDO Class - Selecting Different Tables with same name fields

I have two tables im mysql, for example on table is called worker and the other one is called interventions.

The Table "worker" has 3 fields:

  • ID
  • Name
  • Email

The table "interventions" has 3 Fields as well:

  • ID
  • NameId
  • Description

I want to cross data between both tables. Here is the code of PDO Class:

    class PdoInsideClass {
    public  $ID, $Name , $NameId, $Description ;

    public function __construct() {
        $this->ID = "{$this->ID}";
        $this->Name = "{$this->Name}";
        $this->NameId = "{$this->NameId}";
        $this->Description = "{$this->Description}";
    }
}

The Database Query

$myquery = $database_connection->query( 'SELECT * FROM 
                                        worker, interventions
                                        WHERE worker.ID=interventions.NameId
                                        ');
$myquery->setFetchMode(PDO::FETCH_CLASS, 'PdoInsideClass');

Table and the Loop

<table>
    <tr>
        <th>ID of the Intervention</th>
        <th>Description Of the Job</th>
        <th>Id of the Worker</th>
        <th>Name of the Worker</th>
    </tr>   
    while ($result = $myquery->fetch()) {
        echo "<tr>"; //OPEN TABLE
        echo '<td>' . $result->ID . "</td>"; //ID of the Intervention
        echo '<td>' . $result->Description . "</td>"; //Description Of the Job
        echo '<td>' . $result->ID . "</td>"; //Id of the Worker
        echo '<td>' . $result->Name . "</td>"; //Name of the Worker
        echo "</tr>"; // CLOSE TABLE

    }
    ?>
</table>

The problem: When I print this i get two fields that are the same (ID of the Intervention AND Id of the Worker);

The question is: How can i select the ID of the table that I want ? By default the ID selected is the first table of the query -- 'SELECT * FROM worker, interventions' -- in this case is the table worker.

Solved awesers that I'm not looking for: Querying this -> $this->NameId = "{$this->NameId}"; Insted of this -> $this->ID = "{$this->ID}";

My goal is: I want to know if its possible to querying diferent tables with fields with same names, and specifing the order and not defined by what i put first in the query.






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire