473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PHP and MySQL Table Joins

I am having a hard time with joins - my following code displays:

..member_name .gender

instead of the actual data - I've been reading through my PHP and
MySQL manuals - the MySQL manual tells me how to form the syntax but
it is always shown in MySQL interactive mode and not using PHP code so
I have to try and figure it out in PHP (I've already opened mysql and
selected the database):

$php_SQL = "SELECT basics.member_name, personal.gender FROM basics,
personal WHERE basics.member_name = personal.member_name";
$php_resultID = mysql_query($php_SQL, $php_linkID);
while ($php_row = mysql_fetch_row($php_resultID))
{
print "$php_row->basics.member_name
$php_row->personal.gender<BR>";
}

Jul 16 '05 #1
3 3459
Ralph Freshour <ra***@primemail.com> wrote in message
news:<nr********************************@4ax.com>. ..

I am having a hard time with joins - my following code displays:

.member_name .gender

instead of the actual data -

$php_SQL = "SELECT basics.member_name, personal.gender FROM basics,
personal WHERE basics.member_name = personal.member_name";
$php_resultID = mysql_query($php_SQL, $php_linkID);
while ($php_row = mysql_fetch_row($php_resultID))
{
print "$php_row->basics.member_name
$php_row->personal.gender<BR>";
}


Of course. Variable names (and object fields are variables) cannot
contain periods. Try aliasing; on an unrelated topic, use JOIN rather
than WHERE to link the two tables:

$php_SQL = 'SELECT basics.member_name AS member_name, ' .
'personal.gender AS gender ' .
'FROM basics LEFT JOIN personal ' .
'ON basics.member_name = personal.member_name';
$php_resultID = mysql_query($php_SQL, $php_linkID);
while ($php_row = mysql_fetch_row($php_resultID)) {
echo $php_row->member_name, $php_row->gender, '<BR>';
}

Also, joining on [an unindexed?] text field is a performance drag;
consider joining on an indexed ID field if you have one. (If you
don't, you probably should.)

Cheers,
NC
Jul 16 '05 #2


"Ralph Freshour" <ra***@primemail.com> wrote in message
news:nr********************************@4ax.com...
I am having a hard time with joins - my following code displays:

.member_name .gender
[snip]

$php_SQL = "SELECT basics.member_name, personal.gender FROM basics,
personal WHERE basics.member_name = personal.member_name";
$php_resultID = mysql_query($php_SQL, $php_linkID);
while ($php_row = mysql_fetch_row($php_resultID))
{
print "$php_row->basics.member_name
$php_row->personal.gender<BR>";
}


I rarely (read never!) use the object syntax, but try printing without the
qualifying table name,
I'm sure this should work:

while ($php_row = mysql_fetch_row($php_resultID))
{
print "$php_row[member_name] $php_row[gender]<BR>";
}

then quite likely:

while ($php_row = mysql_fetch_row($php_resultID))
{
print "$php_row->member_name $php_row->gender<BR>";
}

would work too?

Thanks
Mark
---------------------------------------------------------------------------
Windows, Linux and Internet Development Consultant
Email: co*******@scriptsmiths.com
Web: http://www.scriptsmiths.com
---------------------------------------------------------------------------

Jul 16 '05 #3
I think I've got the 3rd table included ok - I say I think because
while this specific query is returning 5 records - that is correct in
that is how many are less than 30 days old so that seems to be working
- however, the other fields such as age, filename are displaying as
the same data - from record one - member_name is the only unique data
probably from the GROUP BY clause - each member has a different age in
the personal table yet the first record age col in personal is 28 and
all 5 records display 28 for age - how do I get each member's age from
the personal table to display???

$php_SQL = "SELECT ".
"basics.account_creation_date AS account_creation_date, ".
"basics.member_name AS member_name, ".
"basics.displayed_member_name AS displayed_member_name, ".
"photos.filename1 AS filename, ".
"personal.age AS age ".
"FROM basics, personal, photos ".
"WHERE account_creation_date >= DATE_SUB(NOW(), INTERVAL 30
DAY) ".
"GROUP BY member_name";

$php_resultID = mysql_query($php_SQL, $php_linkID);
while ($php_row = mysql_fetch_object($php_resultID))
{
print "<TR>";
print "<TD>";
print $php_row->filename . ", ".
$php_row->displayed_member_name . ", ".
$php_row->age . ", ".
"<BR>";
print "</TD>";
print "</TR>";
}

On 1 Sep 2003 08:46:24 -0700, ma****@volja.net (lazo) wrote:
Ralph Freshour <ra***@primemail.com> wrote in message news:<nr********************************@4ax.com>. ..
I am having a hard time with joins - my following code displays:

.member_name .gender

instead of the actual data - I've been reading through my PHP and
MySQL manuals - the MySQL manual tells me how to form the syntax but
it is always shown in MySQL interactive mode and not using PHP code so
I have to try and figure it out in PHP (I've already opened mysql and
selected the database):

$php_SQL = "SELECT basics.member_name, personal.gender FROM basics,
personal WHERE basics.member_name = personal.member_name";
$php_resultID = mysql_query($php_SQL, $php_linkID);
while ($php_row = mysql_fetch_row($php_resultID))
{
print "$php_row->basics.member_name
$php_row->personal.gender<BR>";
}


Hi,

when I use joins, I always add alias (SELECT basics.member_name as
alias_1, personal.gender as alias_2 FROM ...), then to display data I
use ...

while ($php_row = mysql_fetch_array($php_resultID))
{
echo '$php_row[alias_1]';
echo '<br>';
echo '$php_row[alias_2]';
echo '<br>';
echo '<br>';
}


Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream...
5
by: red85 | last post by:
hello i have mysql 4.1 with win2000 SP3, i know that it is only an alpha and i don't know if someone else has already posted this problem: when i execute this sql UPDATE tableX SET...
0
by: Eric B. | last post by:
Hi, I'm somewhat new to MySql. I've been using it for a while, but pretty much out of the box setup, and am starting to suffer heavily with my larger tables. I have a table with 5,000,000+...
8
by: wlcna | last post by:
mysql v4.0.16: I had been using mysql with innodb and thought that was fine, until i used it for something requiring a few - perhaps slightly involved - joins, and have now seen the performance...
2
by: JP | last post by:
My company is considering acquisition of a packaged application that's based on My SQL. This would be our first use of MySQL and the CEO is worried about using a "no-name" database (he's a...
1
by: deepak.rao | last post by:
Hi, I have to run the following statement in MySQL. (The script is generated from oracle). ************* SELECT t1.c1, t2.c2, t3.c3 from t1, t2, t3 where t1.flag = 1 and (t1.cx = t2.cx(+)...
4
by: Ted | last post by:
Understand, I have developed a number of applications using RDBMS, including MySQL, PostgreSQL and MS Access, but this is my first experience with MS SQL. I'd bet my bottom dollar that MS SQL...
6
by: onnodb | last post by:
Hi all, While working on an Access UI to a MySQL database (which should be a reasonable, low-cost, flexible interface to the DB, better than web-based, much less costly than a full-fledged .NET...
4
by: Federico | last post by:
Hi everybody, I'm evaluating the possibility of using MySQL 5.0 as a database backend for an application we are doing and I'd like to have hardware requirements rough estimates and/or real world...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.