/    Sign up×
Community /Pin to ProfileBookmark

LEFT JOIN not pulling all data

I have the following query

[CODE]
function getAllSerialData_serials() {
$SERIALS = array();

$query = mysql_query(“SELECT * FROM serials
LEFT JOIN serialowners
ON serials.serialNum =serialowners.serialNum
“);

$num_rows = mysql_num_rows($query);
if($num_rows > 0) {
while($ROW = mysql_fetch_array($query, MYSQL_ASSOC)) {
$SERIALS[] = $ROW;
}
}
return $SERIALS;
}

[/CODE]

serials table includes id (primary), serialNum
serialowners table includes id(primary), serialNum, fName, lName, un, pw, email, etc

In a loop I display the data

[CODE]
$serialData = getAllSerialData_serials();

foreach($serialData as $serial) { ?>

<tr class=”smFont”><td rowspan=”3″ valign=”top”><?=$serial[‘serialNum’];?></td>

<td rowspan=”3″>

<?=$serial[‘lName’];?>, <?=$serial[‘fName’];?><br />
Email: <a href=”mailto:<?=$serial[’email’];?>” class=’listTitle’><?=$serial[’email’];?></a><br />
</td>

etc, etc….

[/CODE]

I want to join these two tables based on serialNum. There will always be a serialNum in the serials table, but not necessarily in the serialowners table.

When I run the query, it is pulling all the data for an entry that has serialNum in both tables, but will not display serialNum from the serials table if there is no joined data in the serialowners table.

I do not understand why it is not displaying the serialNum from the serials table, where there is no join data in the serialowners table.

The loop is displaying the correct number of rows, but rows are blank with no serialNum in the other rows.

Can someone make sense of this for me please?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 21.2010 — A possibility is that since both tables have a column named serialNum, the null value from the owners table is the one returned by $serial['serialNum']. This might be avoided by explicitly listing the columns you need in the query instead of grabbing all via the "*" operator. If needed, assign them aliases to be used as the array indexes:
[code=php]
$sql = "SELECT serials.serialNum AS sn, [...etc...] FROM . . ."
//
// . . .
//
echo $serial['sn']
[/code]
×

Success!

Help @waMP spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...