Unknown Column in Field List

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Skiddles2010
Forum Newbie
Posts: 19
Joined: Tue Jul 06, 2010 11:05 pm

Unknown Column in Field List

Post by Skiddles2010 »

The error:
Unknown column 'item' in 'field list'
The relevant code:

Code: Select all

$result = mysql_query("SELECT $add_type " . "_name FROM t_" . "$add_type WHERE $obj_name = $add_type" . "_name") or die(mysql_error());
Basically I'm trying to append "_name" to the $add_type string, so that it works something like this:

$add_type = "school";
SELECT school_name FROM t_school ...

I need to be able to append the _name and pre-pend(?) t_ so that this code can dynamically process inputs from a number of different types of forms.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Unknown Column in Field List

Post by AbraCadaver »

I assume that $add_type = item in this case? It is probably the extra space you have after the first $add_type in the query.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Skiddles2010
Forum Newbie
Posts: 19
Joined: Tue Jul 06, 2010 11:05 pm

Re: Unknown Column in Field List

Post by Skiddles2010 »

Ahh, thanks. Works.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Unknown Column in Field List

Post by Weirdan »

It's easier to use curly braces in such cases (assuming the add_type and obj_name variables are properly filtered/encoded):

Code: Select all

$result = mysql_query("
       SELECT {$add_type}_name 
       FROM t_{$add_type} 
       WHERE {$obj_name} = {$add_type}_name
") or die(mysql_error());
Skiddles2010
Forum Newbie
Posts: 19
Joined: Tue Jul 06, 2010 11:05 pm

Re: Unknown Column in Field List

Post by Skiddles2010 »

Nice one Weirdan.
Post Reply