Jump to content

Recommended Posts

Hi guys,

 

I'm having some problem with a piece of code, which should be displaying 5 things from 2 different tables. Here's my code:

 

      <?

   $showevent = mysql_query("SELECT * FROM fights WHERE event='$viewpick' AND id IN (SELECT fight_id FROM betting WHERE user_id='$user->id') ORDER by id ASC");
   while($the=mysql_fetch_object($showevent)){ 
   


  echo "
	  <tr><td class='subtableheader' colspan='1'>Red Corner</td>
	  <td class='subtableheader' colspan='1'>Blue Corner</td>
	  <td class='subtableheader' colspan='1'>Event</td>
	  <td class='subtableheader' colspan='1'>My Pick</td>
	  <td class='subtableheader' colspan='1'>Winner</td></tr>
	  <tr><td class='profilerow' colspan='1'>$the-fighter1</td>
	  <td class='profilerow' colspan='1'>$the->fighter2</td>
// Above 2 are fetched from the FIGHTS table. . 
	  <td class='profilerow' colspan='1'>$viewpick</td>
//Below 2 should be fetched from BETTING table, but should coincide with the results from the FIGHTS table.
	  <td class='profilerow' colspan='1'>$fighter_id</td>
	  <td class='profilerow' colspan='1'>$winner</td></tr>


   </tr>";
}

 

Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/265179-fetch-from-two-tables-without-array/
Share on other sites

That's because $the-> is used when fetching from the FIGHTS table. . . $viewpick is already determined in above code. The ones without any $the-> atall are the ones i need to be called from the BETTING table, But i Cannot figure out how to call from 2 tables in the same query, as the FIGHTS info needs to coincide with the BETTING info.

 

So - Fighter1 vs Fighter 2 & Event are determined from the fights table, i then need to fetch the pick of which that user chose in the BETTING table (fighter_id) & the winner of that fight, again from the BETTING Table (winner), The two would be aligned by the fight_id.

I am selecting from FIGHTS table WHERE the event column is $viewevent (this is determined by which event is selected by user) AND id (of the fight) column is in the BETTING table WHERE the users id is $user->id. Then order it by id.

 

That sorts the first 3 columns in my code ($the-fighter1, $the-fighter2 & $viewevent). The next two i need fetching from the BETTING table.

 

So i would use. .

 

Select from BETTING table WHERE the event column is $viewevent AND the id of the fight is the same id as the corresponding result.

I've managed to do it a different way now, thanks for the help.

 

Can anyone tell me if the bold part of this code is valid. . .

 

 $showevent = mysql_query("SELECT * FROM fights WHERE event='$viewevent' [b]AND id IN (SELECT fight_id FROM betting WHERE user_id='$user->id')[/b] ORDER by id ASC");
while($the=mysql_fetch_object($showevent)){  

Hi,

 

I'm trying to fetch a number of columns from two different tables in a database. I'm using the following code to fetch the first set of info. .

	   $showevent = mysql_query("SELECT * FROM fights WHERE event='$viewevent' AND id IN (SELECT fight_id FROM betting WHERE user_id='$user->id') ORDER by id ASC");
   while($the=mysql_fetch_object($showevent)){ 

I need to use this code to fetch the second set:

	   $showpick = mysql_query("SELECT * FROM betting WHERE event='$viewevent' AND user_id='$user->id'");
   while($pick=mysql_fetch_object($showpick)){ 

 

I need the results in a table like so. .

 

Red Corner - Blue Corner - Event - My Pick - Winner

$the->f1 - $the->f2  - $viewevent - $pick->fighter_id - $pick->winner

 

I need the results from the first query to MATCH the results from the second query. These can be matched by the fight_id from both tables. I just have no clue how to write the query for it.

Try this: (1 query)

$sql = "SELECT f.f1,f.f2,p.fighter_id,p.winner FROM fights AS f JOIN betting AS p ON f.id = p.fight_id WHERE f.event='$viewevent' AND p.user_id='$user->id' ORDER by f.id ASC"
$showevent = mysql_query($sql) or trigger_error(mysql_error());

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.