Jump to content

PHP OOP PDO MySQL Select from two tables


spertuit
Go to solution Solved by Jessica,

Recommended Posts

I'm attempting to select all records from a table than loop through the results and select all records from another table where tableid is equal to the id of the results of the first query.

So basically select all from first table, while you have results, select all from second table where tableid is equal to first results current id, than pass all this into an array for use later.

I'll take a stab at it, but I'm trying to get properly written code in OOP style using PDO. Its basically a one to many relationship, 1 record in table1 will have multiple records in table2. 

public function showLog($table){
		
		$sql="SELECT * FROM $table";
		$q= $this->conn->query($sql) or die("failed!");
		
		while($r = $q->fetch(PDO::FETCH_ASSOC)){
			$sql="SELECT * FROM :table WHERE id = :id";
			$q = $this->conn->prepare($sql);
			$q->execute(array(':table'=>"vessels",':id'=>$id));
			$data = $q->fetch(PDO::FETCH_ASSOC);
		}
		return $data;	
	}
Edited by spertuit
Link to comment
Share on other sites

im not sure if a join will work. lets try simpler for now.

 

logs               vessels

------              ---------

id                       id

date                 name

vesselID

 

now i want to display a table that will show many logs that display logs.id,logs.date,vessels.name where vesselID = vessels.id

and I'm trying to do it using PDO 

 

I'm close

		public function showLog($table){
		
		$sql="SELECT * from $table";
		$q= $this->conn->query($sql) or die("failed!");
		while($r = $q->fetch(PDO::FETCH_ASSOC)){
					$sql="SELECT * FROM 'vessels' WHERE id = :id";
					$q = $this->conn->prepare($sql);
					$q->execute(array(':id'=>$r['logVesselID']));
					$data = $q->fetch(PDO::FETCH_ASSOC);
					$data[] = $r;
		}
		return $data;
			
	}
Edited by spertuit
Link to comment
Share on other sites

You guys are tough

		public function showLog($table){
		
		$sql="SELECT * FROM `dailyMasterLogs` AS q LEFT JOIN `vessels` AS i ON ( q.logVesselID = i.id)";
		$q= $this->conn->query($sql) or die("failed!");
		while($r = $q->fetch(PDO::FETCH_ASSOC)){
					$data[]=$r;
		}
		return $data;

Can you get me advice to clean this up or does it look ok? It works, I'm just trying to get it the best I can.

Link to comment
Share on other sites


		public function showLog($table){
		
		$sql="SELECT * FROM `dailyMasterLogs` AS logs LEFT JOIN `vessels` AS vessel ON ( logs.logVesselID = vessel.id) ORDER BY logs.id DESC";
		$q= $this->conn->query($sql) or die("failed!");
		while($r = $q->fetch(PDO::FETCH_ASSOC)){
					$data[]=$r;
		}
		return $data;

Here is the final. I appreciate the help, and I'll check out that tutorial.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.