Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
James M.
Forum Contributor
Posts: 119 Joined: Wed Mar 31, 2004 6:41 pm
Location: Tallahassee
Post
by James M. » Sat Apr 17, 2004 11:55 pm
Hi, i'm doing this project from a tutorial to learn php and I can't find the problem with this function, MySQL give me the error:
getEvents fatal error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'clubs, events, areas, types WHERE clubs.id=events.eclub AN
can anyone help?
heres the code:
Code: Select all
function getEvents($club_id=0, $range=0, $area=0, $type=0)
{
global $link;
$query="SELECT clubs.name, events.*, areas.area as areaname,
types.type as typename";
$query .="FROM clubs, events, areas, types WHERE ";
$query .="clubs.id=events.eclub
AND events.area=areas.id
AND events.type=types.id";
if( ! empty($club_id) && $club_id !="ANY")
{
$query .="AND events.eclub='$club_id'";
}
if( ! empty($range))
{
$query .="AND events.edate >='$rangeї0]' AND
events.edate<='$rangeї1]'";
}
if(! empty($area) && $area !="ANY")
{
$query .="AND events.area='$area'";
}
if( ! empty($type) && $type !="ANY")
{
$query .="AND events.type='$type'";
}
$query .="ORDER BY events.edate";
$result=mysql_query($query, $link);
if( ! $result)
{
die("getEvents fatal error: ".mysql_error());
}
$ret=array();
while($row=mysql_fetch_array($result))
{
array_push($ret, $row);
}
return $ret;
}
if you need any more information i'll be glad to give it.
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sun Apr 18, 2004 2:01 am
Before running the query, try echo $query as text-output. Might help you debug it. I spot one problem i think;
Code: Select all
if( ! empty($type) && $type !="ANY")
{
$query .="AND events.type='$type'";
}
You need spaces before AND in those...
steve2004
Forum Newbie
Posts: 14 Joined: Fri Apr 16, 2004 1:18 pm
Post
by steve2004 » Sun Apr 18, 2004 4:39 am
hi
period after events.*
James M.
Forum Contributor
Posts: 119 Joined: Wed Mar 31, 2004 6:41 pm
Location: Tallahassee
Post
by James M. » Sun Apr 18, 2004 1:24 pm
thanks for the help, i found the problem, there was a space missing
Code: Select all
$query="SELECT clubs.name, events.*, areas.area as areaname,
types.type as typename"; // <--need space after 'as typename'
saw it after JAM was talking about spaces, thanks.
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Mon Apr 19, 2004 9:42 am
Glad it worked out. What I saw was that if you sendt both $club_id and $range to the function, you'd have the spaces issue. You are perhaps not using it like that, but I'd like to mention it as it wasn't obvious on how the function works.
Code: Select all
// <snip>
if( ! empty($club_id) && $club_id !="ANY")
{
$query .="AND events.eclub='$club_id'"; // bad
$query .=" AND events.eclub='$club_id'"; // better
}
if( ! empty($range))
{
$query .="AND events.edate >='$rangeї0]' AND events.edate<='$rangeї1]'"; // bad
$query .=" AND events.edate >='$rangeї0]' AND events.edate<='$rangeї1]'"; // better (etc...)
}
// <snip>
Adding a space before each AND in the if-clauses makes that work and the space after 'typename' can be ignored.