Forum Moderators: coopster

Message Too Old, No Replies

Limiting a query using get data

Limiting a query using get data

         

scaleypate

8:49 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



In the following code, I send a variable using the GET method. I then try to limit the query to return only rows that contain that variable in the department table departmentId field. However, it keeps looking for a field named with the value of the variable. For example, the column name is departmentId. The variable is dept_id. The value of dept_id is KKKK. The error I get says 'Unknown column 'KKKK' in 'where clause'.

Thanks!

Dawn

<?php
if (isset($_GET['dept_id'])) {
$query = "SELECT * FROM Department, Division WHERE Department.departmentId = Division.departmentId AND Division.departmentId = {$_GET['dept_id']} ORDER BY Department.department";
$query_result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($query_result)) {
print "<a href=\"div_main.php?div_id={$row[1]}\"> {$row[1]}</a>";
}
} else {
print "ARRRGGGG";
}
?>

Philosopher

9:21 pm on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why don't you add

echo $query;

directly after you define your query statement and before you run the statement to see what it actually looks like and post the results here.

P.S. Welcome to Webmaster World!

mcibor

10:04 pm on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's not table name, then it should be in single quotes ' ':

<?php
if (isset($_GET['dept_id'])) {
$query = "SELECT * FROM Department, Division WHERE Department.departmentId = Division.departmentId AND Division.departmentId = '".$_GET['dept_id']."' ORDER BY Department.department";
$query_result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($query_result)) {
print "<a href=\"div_main.php?div_id={$row[1]}\"> {$row[1]}</a>";
}
} else {
print "ARRRGGGG";
}
?>

This should help
Michal Cibor