PHP forums, MySQL forums, Web Development resources

Home PageHome    PHP ResourcesTopic List    FAQFAQ    SearchSearch    MemberlistMemberlist    UsergroupsUsergroups 
 RegisterRegister
    ProfileProfile    Log in to check your private messagesLog in to check your private messages    Download the RSS Reader RSS Feed Download the RSS Reader RSS for this forum Log inLog in 

PHP Forum :: MySQL Forum :: Java Script Forum



Need Help Connecting to 2nd Database Table

 
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General
View previous topic :: View next topic  
Author Message
Dave 5



Joined: 08 May 2004
Posts: 108
Location: Seattle

PostPosted: Mon Nov 08, 2004 11:02 am    Post subject: Need Help Connecting to 2nd Database Table Reply with quote

This is driving me nuts! It should be so simple, yet I can't find the problem.

I have a really nice script that includes scripts for displaying database tables with varying table row colors and giving them sortable columns. It's a little too complex for me to understand, but it works quite well.

However, I now need to connect it to a second table, and I'm having no success at all.

The second table I need to connect to is named famarea. I need to access a field named IDParent. The tables famarea and cia_people share a field named IDArea in common.

Consider this portion of my source code:

[code:1]
$res = mysql_query("SELECT IDArea, Name, Pop, Nationality, NationalityPlural, NationalityAdjective
FROM cia_people as C
WHERE C.Nationality is not null
ORDER BY C." . $order . $direction);
[/code:1]

I need to change it to something like this:

[code:1]
$res = mysql_query("SELECT IDArea, Name, Pop, Nationality, NationalityPlural, NationalityAdjective
FROM cia_people as C, famarea as F
WHERE C.Nationality is not null AND C.IDArea = F.IDArea AND F.IDParent = 'xeu'
ORDER BY C." . $order . $direction);
[/code:1]

But every effort I make to introduce table famarea yields this error message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\sites\geoworld\about\guide\world\eur\remote.php on line 96

Where line 96 is while ($row = mysql_fetch_array ($res)) {

I'm so sick of that error message. I've been seeing it so many times, and it doesn't really explain anything; what does it mean? I posted the entire code below. Does anyone have any suggestions? Is there some way of modifying or simplifying this script so I don't have to jump through so many hoops when trying to do something as simple as join with another table?

Thanks.

[code:1]
<head>[DATABASE CONNECTION]</head>
<body>
<div class="formdiv">
<form action="remote.php" method="GET">
<select name="order">
<option value="1">Country, etc.</option>
<option value="2">Population</option>
<option value="3">Nationality</option>
<option value="4">Nationality: Plural</option>
<option value="5">Nationality: Adjective</option>
</select>
<input type="radio" name="direction" value="0">+
<input type="radio" name="direction" value="1">-
<input type="submit" name="submit" value="Submit">
</form>
</div>
<?php
$colors = array( '#eee', '', '#ff9', '', '#cff', '', '#cfc', '' );
$n=0;
$size=count($colors);

$result = mysql_query('select count(*) from cia_people');
if (($result) && (mysql_result ($result , 0) > 0)) {
// continue here with the code that starts
//$res = mysql_query ("SELECT * FROM type.....
} else {
die('Invalid query: ' . mysql_error());
}
{

$order = isset($_REQUEST['order']) ? intval($_REQUEST['order']) : 0;
switch($order)
{
case 1:
$order = 'Name';
break;
case 2:
$order = 'Pop';
break;
case 3:
$order = 'Nationality';
break;
case 4:
$order = 'NationalityPlural';
break;
case 5:
$order = 'NationalityAdjective';
break;
case 6:
default:
$order = 'Name';
break;
}
if (isset($_REQUEST['direction']) && intval($_REQUEST['direction'])) {
// if (isset($_REQUEST['direction']) && intval($_REQUEST['direction'])) {
$direction = ' DESC';
} else {
$direction = '';
}



$res = mysql_query("SELECT IDArea, Name, Pop, Nationality, NationalityPlural, NationalityAdjective
FROM cia_people as C
WHERE C.Nationality is not null
ORDER BY C." . $order . $direction);



echo '<table class="sortphp" id="tab_cia_people_peo">
<thead>
<tr><th>Country</th><th>X</th></tr>
</thead>
<tbody>';
//<!-- BeginDynamicTable -->
$rowcounter=0;
while ($row = mysql_fetch_array ($res)) {
$c=$colors[$rowcounter++%$size];
echo "<tr style=\"background-color:$c\"><". $_SERVER['PHP_SELF'] .'?id='. $row['IDArea'] .">
<td>". $row['Name'] ."</td>
<td>&nbsp;</td></tr>\n";
}
}
?>
</tr>
</tbody>
</table>
</body>
</html>
[/code:1]
Back to top
View user's profile Send private message Visit poster's website
lostboy



Joined: 02 May 2004
Posts: 6059
Location: toronto, canada

PostPosted: Mon Nov 08, 2004 7:25 pm    Post subject: Reply with quote

check the sql statement for wellformed ness

[code:1]
$res = mysql_query("SELECT IDArea, Name, Pop, Nationality, NationalityPlural, NationalityAdjective
FROM cia_people as C
WHERE C.Nationality is not null
ORDER BY C." . $order . $direction) or die (error: ".mysql_error());
[/code:1]

I think you see that the syntaxt is a little screwy at the end

try this:


ORDER BY C." . $order ." ". $direction)
Back to top
View user's profile Send private message Send e-mail AIM Address MSN Messenger
papercrane



Joined: 16 Jul 2004
Posts: 637
Location: California, US

PostPosted: Mon Nov 08, 2004 8:26 pm    Post subject: Reply with quote

This is happening because the query isn't going through correctly. You need to check for errors and output the *real* mysql error to see the problem. COmehting like this:

[code:1]$res = mysql_query('...');
if (!$res) {
die(mysql_error());
}[/code:1]

You may also want to use the ON syntax which mysql uses for joins (when you select from more than one table, it's called a join).

[code:1]'
SELECT
IDArea
, Name
, Pop
, Nationality
, NationalityPlural
, NationalityAdjective
FROM
cia_people as C
LEFT JOIN famarea as F ON C.IDArea = F.IDArea
WHERE
C.Nationality is not null
AND F.IDParent = "xeu"
ORDER BY
C.'.$order.' '.$direction;
$res = mysql_query($query);
if (!$res) {
die('Error running query: '.mysql_error().'<br/>
<pre>'.$query.'</pre><br/>');
}[/code:1]

I think you were also missing a space between $order and $direction.

Whenever you get errors like that, output the query and try to run it in another program, such as the mysql command-line, PHPMYAdmin, or Mysql Control Center (The latter is my preferred method).
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
Dave 5



Joined: 08 May 2004
Posts: 108
Location: Seattle

PostPosted: Tue Nov 09, 2004 1:33 am    Post subject: Reply with quote

Hooray - it works! I've been working on this for about a day and struck out on three different forums. Thanks for figuring it out for me!
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
WeberTrivia Questions WeberTrivia Questions
 Think you are smart? Prove it!. Try your skills with these questions :
 WeberTrivia QuestionsRecursive arrays and multi-dimensional arrays are one and the same. (PHP and MySQL)
 WeberTrivia QuestionsThe \"cache_dir\" tag of the squid configuration has a default of /var/spool/squid. (Linux)

WeberTrivia Questions



PHP Code Examples
 Stream diffrent sizes of images from a single image to save disk space.
 JavaScript dropdown list menu to switch any page.
 Dump the contents of a PHP variable in html format with a recursive list of subfolders and files from a given root directory.
 PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside.
 Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff.
 Building a Dynamic Form using Javascript and innerHTML. Add form elements in realtime without refreshing the page.
 A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Post new topic   Reply to topic    WeberForums.com Forum Index -> PHP General All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
PHP Forum :: MySQL Forum :: Java Script Forum