[PHP] Why does this SQL not work?

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

This sql statement seems to just return "category" despite the fact its a SMALLINT(3) field.

I have checked spelling and variables so its nothing to do with that.

Code:
$result = mysql_query("SELECT 'id','name','link','category' FROM software ORDER BY 'id' DESC LIMIT $offset,$rowsPerPage") or die('Error1, query failed');

if ( mysql_num_rows($result) > 0 ) {
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		WriteRow($row['id'],$row['name'],$row['link'],$row['category'],$tabs);
	}

Code:
function WriteRow($id,$name,$link,$category,$tabs){
	echo $category;
}

Why is that? and how do I fix it?

Thanks.
 
Single quotes are for quoting values; use backticks (`) for quoting identifiers. As it is you're just selecting (and ordering by) static strings.

Code:
SELECT `id`, `name`, `link`, `category` FROM `software` ORDER BY `id` DESC LIMIT '$offset', '$rowsPerPage'
 
Last edited:
Whilst I am here, this is ok to do isn't it? (security):

Code:
if(intval($_GET['category'])){
	$getCat = "WHERE `category` = " . intval($_GET['category']) . " ";
}else{
	$getCat = "";
}

...and then inserting that into the middle of an SQL statement.
 
Yup. It's safe as long as there's no chance of characters that could alter the structure of the statement finding their way in, which intval() prevents :)
 
Back
Top Bottom