iabbott
iabbott

Reputation: 881

Getting rows from 2 tables

I have 2 tables:

tblDowntime
downtimeID - downtimeDescription
1          - dt_desc
2          - dt_desc2


tblMachineArea
areaID     - downtimeID - areaDescription
1          - 1          - area1
2          - 2          - area2

I know the downtimeDescription, so what do I have to do to get the corresponding areas from tblMachineArea?

I can get them from the ID, but my listbox will only have the description

I have got this answer for INSERT, but couldn't adapt it...unless I'm missing something really obvious...

My other question is what join to use... I have tried inner join with machineID = 1, it returns all the rows in tblMachineArea but with different machineID's depending on if I use 1 or 2 in the query.... doesn't make much sense to me

EDIT: the query I have run is

SELECT areaID, tblMachineArea.downtimeID, tblDowntime.downtimeDescription, areaDescription
FROM tblDowntime
INNER JOIN tblMachineArea ON tblMachineArea.downtimeID = 1;

image: (please forgive the table names and fields are slightly different, so I have updated the originals) enter image description here

Any help would be appreciated

Upvotes: 0

Views: 57

Answers (1)

null
null

Reputation: 3517

You could avoid using joins and run two queries.

First get the ID of the machine and store that as a variable. THen use that variable to get the info you need.

Also, the link you mentioned doesn't use prep`red statements, it's best to use that as a strategy if you can to improve security.

This is my PHP function for registering a user, you can see how the prepared statement is structured.

//function for registering a new user and adding their details to the database
public function register($username, $password, $email){

$time       = time();
$ip         = $_SERVER['REMOTE_ADDR'];
$emailCode = sha1($username + microtime());
$password   = sha1($password);

//prepare statement and bind values
$query  = $this->db->prepare("INSERT INTO `users` (`username`, `password`, `email`, `ip`, `time`, `emailCode`) VALUES (?, ?, ?, ?, ?, ?) ");

$query->bindValue(1, $username);
$query->bindValue(2, $password);
$query->bindValue(3, $email);
$query->bindValue(4, $ip);
$query->bindValue(5, $time);
$query->bindValue(6, $emailCode);

try{
    $query->execute();

    //function stuff here removed to save space

}catch(PDOException $e){
    die($e->getMessage());
}   
 }//end register function

Upvotes: 1

Related Questions