open two tables at once??
Moderator: General Moderators
open two tables at once??
how can you pull info out of two tables at the same time??
using some sort of join: http://dev.mysql.com/doc/mysql/en/JOIN.html
This information is for MySQl, but should be pretty similar for any database supporting SQL language.
This information is for MySQl, but should be pretty similar for any database supporting SQL language.
Code: Select all
<?
$db_name = "database";
$table_name = "members";
$db_name = "database";
$table2_name = "adverts";
$connection = @mysql_connect("localhost", "user", "password")
or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection)
or die("Couldn't select database.");
$sql = "SELECT mem_userid, mem_username, mem_email, adv_picture, adv_username
FROM $table_name, table2_name
WHERE mem_userid > '30001'
ORDER BY mem_userid
";
$result = @mysql_query($sql,$connection)
or die("Couldn't execute query.");i need the mem_userid to equal the adv_username between the two tables
where am i going wrong
A simple way to pull data out of two table at the same time, is to list both of them in the "FROM" clause. For example, say I have 2 tables, one called "computer_info" and another called "personal_info", and I want to get the operating system and hair colour of a person with a particular username. The query I could use for that is:
If I just wanted to get all os's and all hair colours, I could just remove the WHERE clause.
Code: Select all
SELECT
os,
hair_colour
FROM
computer_info,
personal_info
WHERE
computer_info.username = 'joe_user' AND
personal_info.username = 'joe_user'Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Code: Select all
$sql = "SELECT mem_userid, mem_username, mem_email, mem_joindate, mem_sex, mem_country, adv_username, adv_picture
FROM $table_name,
FROM $table2_name
WHERE table_name.mem_userid > '30001' AND
WHERE table2_name.adv_username = 'mem_username'
ORDER BY mem_userid
";- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
<?php
$sql = "SELECT mem_userid, mem_username, mem_email, mem_joindate, mem_sex, mem_country, adv_username, adv_picture
FROM $table_name,
$table2_name
WHERE table_name.mem_userid > '30001' AND
table2_name.adv_username = 'mem_username'
ORDER BY mem_userid ";
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
<?php
$sql = "SELECT t1.mem_userid, t1.mem_username, t1.mem_email, t1.mem_joindate, t1.mem_sex, t1.mem_country, t2.adv_username, t2.adv_picture
FROM $table_name t1,
$table2_name t2
WHERE t1.mem_userid > '30001' AND
t2.adv_username = t1.mem_username
ORDER BY t1.mem_userid ";
?>