PDA

View Full Version : Table Join Invalid Query


user55
07-25-2007, 12:04 AM
Hi,

I have this table join below and I am getting an invalid query. How can I put the 15th line ($sql .= " group.id = master.id\n"; and the 17th line ($sql .= " where ".$currentrow_sql ; together. Is it even possible to do that? Any help would be appreciated.



$sql .= " Select\n";
$sql .= " group.`id`,\n";
$sql .= " group.`amount`,\n";
$sql .= " group.`company`,\n";
$sql .= " group.`otherfee`,\n";
$sql .= " group.`total`,\n";
$sql .= " master.`id`,\n";
$sql .= " master.`section`,\n";
$sql .= " master.`year`,\n";
$sql .= " master.`location`\n";
$sql .= " From\n";
$sql .= " group group,\n";
$sql .= " master master\n";
$sql .= " Where\n";
$sql .= " group.id = master.id\n";
if ($currentrow_sql != "") {
$sql .= " where ".$currentrow_sql ;
}
$result = mysql_query($sql) or die("Invalid query");
$row = mysql_fetch_array($result);
}


_______________
Sara

guelphdad
07-25-2007, 12:37 AM
it looks like you have a table named GROUP. GROUP is a reserved word and should not be used as a table name, database name or column name.

user55
07-25-2007, 12:45 AM
Hi,

Thanks for catching that. Its the wrong code anyway. The two tables are "ebond" and "master".Here is the right code:


$sql .= " Select\n";
$sql .= " ebond .`id`,\n";
$sql .= " ebond .`amount`,\n";
$sql .= " ebond .`company`,\n";
$sql .= " ebond .`otherfee`,\n";
$sql .= " ebond .`total`,\n";
$sql .= " master.`id`,\n";
$sql .= " master.`section`,\n";
$sql .= " master.`year`,\n";
$sql .= " master.`location`\n";
$sql .= " From\n";
$sql .= " ebond ebond,\n";
$sql .= " master master\n";
$sql .= " Where\n";
$sql .= " ebond .id = master.id\n";
if ($currentrow_sql != "") {
$sql .= " where ".$currentrow_sql ;
}
$result = mysql_query($sql) or die("Invalid query");
$row = mysql_fetch_array($result);
}


________________

Sara

guelphdad
07-25-2007, 01:45 AM
first I'd say you don't need the \n in the query at all.
second actually use mysql_error() so you can see what the error is:

$result = mysql_query($sql) or die("Invalid query") . mysql_error();


you have an error in two places where you have not closed off the \n from the rest of the code on the line.

again completely unnecessary in my opinion. you also don't have to write your code as such:

$sql .= " ebond .`id`,\n";
$sql .= " ebond .`amount`,\n";

but can just offset each new item on a new row so you can keep track of it:


$sql = "SELECT
ebond.id,
ebond.amount
etc. etc.
"