Jump to content

retrieving data from more than one table


dr_overload

Recommended Posts

I currently use this script to retrieve data from the one table

<?php
mysql_connect("**", "**", "**") or die(mysql_error());
mysql_select_db("**") or die(mysql_error());
$data = mysql_query("SELECT * FROM world_cup WHERE username ='$username'")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
if ( $info['a'].$info['b'] == $info['c'].$info['d'] ) {
echo $info['e'];
?>

What would I have to change in the script if 'a' and 'b' were in a second table. ie world_cup_scores?

Any help is most appreciated

Link to comment
Share on other sites

[!--quoteo(post=378177:date=May 29 2006, 03:29 PM:name=dr_overload)--][div class=\'quotetop\']QUOTE(dr_overload @ May 29 2006, 03:29 PM) [snapback]378177[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I currently use this script to retrieve data from the one table

<?php
mysql_connect("**", "**", "**") or die(mysql_error());
mysql_select_db("**") or die(mysql_error());
$data = mysql_query("SELECT * FROM world_cup WHERE username ='$username'")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
if ( $info['a'].$info['b'] == $info['c'].$info['d'] ) {
echo $info['e'];
?>

What would I have to change in the script if 'a' and 'b' were in a second table. ie world_cup_scores?

Any help is most appreciated
[/quote]
Assuming you'll be linking the second table to the first table by some type of id:
[code]
Table1: world_cup
table_id INTEGER(20),
info_c    <whatever>,
info_d    <whatever>,
info_e    <whatever>
[/code]

[code]
Table2: world_cup_scores
table_id INTEGER(20), -- this value will reference Table1 table_id as a foreign key
info_a  <whatever>,
info_b <whatever>
[/code]

your select statement:
[code]
$sql = " SELECT world_cup.*,world_cup_scores.* FROM world_cup";
$sql .= " LEFT JOIN world_cup_scores ON world_cup.table_id = world_cup_scores.table_id"
$sql .= " WHERE username ='$username'";
[/code]

The above will retrieve all the work cup games even if there are no scores associated to those games yet.



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.