Jump to content

Recommended Posts

I have 2 tables in my database that I want to pull the information out and show it on a page relative to s username.

 

What would be the code I need to insert in to the SELECT FROM section of the following code to join a table called 'users' with a table called ''trainingdata' associated by the 'username'

<table>
<tr>
    	<th>Name</th>   
        <th>Username</th>
        <th>Email</th>
        <th>Training Required</th>
        <th>Date Submitted</th> 
    </tr>

<?php
$sql = "SELECT `name`, `username`, `email` FROM users WHERE `userlevel`=1";
$query = mysql_query($sql) or die(myslq_error());

while($row = mysql_fetch_assoc($query)){
?>
<tr>
    	<td><?php echo $row['name']; ?></td>
        <td><?php echo $row['username']; ?></td>
        <td><?php echo $row['email']; ?></td>
        <td><?php echo $row['description']; ?></td>
        <td><?php echo $row['date']; ?></td>
    </tr>
<?php
}
?>
</table>

 

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/
Share on other sites

This isnt really a PHP question its a MySQL question. But since your here..

 

SELECT * FROM `users` Inner Join `trainingdata` USING (`username`) WHERE `userlevel` = 1

The above sql will only grab users that have data in both the users AND traningdata tables.

 

If you want to grab all the users anyway and only get the trainingdata if it exists you can replace the Inner Join with Left Join

 

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-996910
Share on other sites

I kind of know what you mean.

 

I have a row of information  in a page called clients.php.

This row shows some of the data held in the 'users' table in the db.

At the end of this row is a 'more' link like this

Name-----------------Username-------------Email-----------------More Info

Bob--------------------Bobby01--------------bob@bob.com------More

 

When the 'More' link is clicked it will go to the clientinfo.php page like this

<?php echo '<a href="clientinfo.php?username='.$row['username'].'">More</a>' ?>

 

I want this new page to get

 

name----username----email

from the users table

 

and

 

description----date

from the trainingdata table

 

Relative to the user in the link.

 

Baffled? I know I am. lol  :shrug:  ::);)

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-996925
Share on other sites

Baffled? No..

$sql = "SELECT 
`users`.`name`,
`users`.`email`,
`users`.`username`,
`trainingdata`.`date`,
`trainingdata`.`description
FROM `users`
Inner Join `trainingdata` USING (`username`)
WHERE `users`.`username`='".$_GET['username']."'
LIMIT 1";
$sql_query = mysql_query($sql) or trigger_error(mysql_error());

if (mysql_num_rows($sql_query) > 0) {
$data = mysql_fetch_assoc($sql_query);
print_r($data);
}

Have a go with that.

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-996928
Share on other sites

wow. how good are you.  :D

 

how do I get that information in a table

 

Heres my code;

<table>
<tr>
    	<th>Name</th>   
        <th>Username</th>
        <th>Email</th>
        <th>Training Required</th>
        <th>Date Submitted</th> 
    </tr>

<?php
$sql = "SELECT `users`.`name`,`users`.`username`,`users`.`email`,`trainingdata`.`description`,`trainingdata`.`date` FROM `users` INNER JOIN `trainingdata` USING (`username`)
WHERE `users`.`username`='".$_GET['username']."' LIMIT 1";
$sql_query = mysql_query($sql) or trigger_error(mysql_error());

if (mysql_num_rows($sql_query) > 0) {
$data = mysql_fetch_assoc($sql_query);

}
?>
<tr>
    	<td><?php echo $row['name']; ?></td>
        <td><?php echo $row['username']; ?></td>
        <td><?php echo $row['email']; ?></td>
        <td><?php echo $row['description']; ?></td>
        <td><?php echo $row['email']; ?></td>
    </tr>

</table>

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-996935
Share on other sites

Think I did it woohoo

<table>
<tr>
    	<th>Name</th>   
        <th>Username</th>
        <th>Email</th>
        <th>Training Required</th>
        <th>Date Submitted</th> 
    </tr>

<?php
$sql = "SELECT `users`.`name`,`users`.`username`,`users`.`email`,`trainingdata`.`description`,`trainingdata`.`date` FROM `users` INNER JOIN `trainingdata` USING (`username`)
WHERE `users`.`username`='".$_GET['username']."' LIMIT 1";
$sql_query = mysql_query($sql) or trigger_error(mysql_error());

if (mysql_num_rows($sql_query) > 0) {
$data = mysql_fetch_assoc($sql_query);
}

?>
<tr>
    	<td><?php echo $data['name']; ?></td>
        <td><?php echo $data['username']; ?></td>
        <td><?php echo $data['email']; ?></td>
        <td><?php echo $data['description']; ?></td>
        <td><?php echo $data['email']; ?></td>
    </tr>

</table>

 

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-996936
Share on other sites


if (mysql_num_rows($sql_query) > 0) {
$data = mysql_fetch_assoc($sql_query);
echo '<tr>
<td>'.$data['name'].'</td>
<td>'.$data['username'].'</td>
<td>'.$data['email'].'</td>
<td>'.$data['description'].'</td>
<td>'.$data['date'].'</td>
</tr>';
} else {
echo '<tr><td colspan="5">Invalid User Data</td></tr>';
}

Would do the trick

 

Edit: Notice my use of the else statement.

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-996938
Share on other sites

Ok lets say "Bob" has 1 entry in the users table but 2 entries in the trainingdata table..

you would need to do something like this..

$sql = "SELECT 
`users`.`name`,
`users`.`email`,
`users`.`username`,
`trainingdata`.`date`,
`trainingdata`.`description
FROM `users`
Inner Join `trainingdata` USING (`username`)
WHERE `users`.`username`='".$_GET['username']."'";
$sql_query = mysql_query($sql) or trigger_error(mysql_error());

if (mysql_num_rows($sql_query) > 0) {
while ($data = mysql_fetch_assoc($sql_query)) {
echo '<tr>
<td>'.$data['name'].'</td>
<td>'.$data['username'].'</td>
<td>'.$data['email'].'</td>
<td>'.$data['description'].'</td>
<td>'.$data['date'].'</td>
</tr>';
}
} else {
echo '<tr><td colspan="5">Invalid User Data</td></tr>';
}

This, however, will produce 2 rows which you dont really want because all the data that is collected from the users table will be repeated.. You need to figure out HOW you would like to display multiple trainingdata rows to fit into your table..

 

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-996946
Share on other sites

Thats fantastic, thanks mate.

 

I see what you mean about duplicating the data. It doesn't look so bad but it is unnecessary. Although I dont think that a user will ask for more than 5 training requests.

 

How would I put a line break between the 2 results.

Link to comment
https://forums.phpfreaks.com/topic/188834-joining-tables/#findComment-997223
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.