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:
$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);
I need to change it to something like this:
Code:
$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);
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:
<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> </td></tr>\n";
}
}
?>
</tr>
</tbody>
</table>
</body>
</html>