Jump to content

Archived

This topic is now archived and is closed to further replies.

zebe

PHP MySQL Query Issue

Recommended Posts

Hi,

I'm trying to run a query like the following example:

[code]
$activity = $HTTP_POST_VARS["activity"];
$sql = "SELECT first, last, email FROM student_involvement WHERE " . $activity . " = 1";
$result = mysql_query($sql) or die(mysql_error());
[/code]

The variable $activity is pulling a column name from a predefined select list of activities. Each one of these fields is an int field and if it is true it will have a 1 false a 0. The query fails:

SELECT first, last, email FROM student_involvement WHERE acapella = 1
EXPORT ERROR:Unknown column 'acapella' in 'where clause'

Does anyone know why this is failing? The columns exist, I can pull general data from that table, but it seems to throw errors when I try to implement a WHERE clause...

Thanks for all of your help in advance![b][/b]

Share this post


Link to post
Share on other sites
Does hardcoding the column name work?

[code=php:0]
$sql = "SELECT first, last, email FROM student_involvement WHERE acapella = 1";
[/code]

Let us know if the query is successful.

Share this post


Link to post
Share on other sites
Just hardcoded it as requested and that is throwing the same error as well... Not sure what's up. The table exists and the columns do as well...

Share this post


Link to post
Share on other sites
Yeah...paste this before your query & let us know if it works:

[code]<?php

$query = mysql_query("SELECT * FROM student_involvement WHERE acapella ='1'");

$res = mysql_fetch_array($query);

print_r($res);exit;

?>[/code]

Share this post


Link to post
Share on other sites
Then the array is blank. It's not retrieving anything from that field in the table. Mind pasting a screenshot of that table?

Share this post


Link to post
Share on other sites
Though the field/column is there....is there any data in it yet?

Share this post


Link to post
Share on other sites
Yes,

I have a form that was placing data into the fields. If a user checked that activity it would store a 1 in the respective field, 0 otherwise. All the int fields are storing are basic binary true false data from checkboxes. When I browse, those fields have 0's and 1's in them, so there is data in there...

Share this post


Link to post
Share on other sites
Yes it is called student_involvement.

I can extract first name last name etc fine, but the problem arises when I try to do a where clause on the integer fields...

Share this post


Link to post
Share on other sites
If you convert the field to varchar, and then run the script again...what are the results? I asume if you add the WHERE clause criteria using a field that is an integer but also set as a primary key, it won't return the error?

Share this post


Link to post
Share on other sites
Changed the `acapella` column to a varchar(1) type, ran it again and got the same unknown column in where clause error...

Share this post


Link to post
Share on other sites
If you would like (Because I'm very damned curious :-P) send me an email and I will give you my AIM sn & I can help troubleshoot this for you.  :) (sammyone@gmail.com)

Share this post


Link to post
Share on other sites
are you connecting to a different database from php that has this table but without the acapella column?

Share this post


Link to post
Share on other sites
[quote author=ryanlwh link=topic=102606.msg407510#msg407510 date=1154462590]
are you connecting to a different database from php that has this table but without the acapella column?
[/quote]

True dat. Check to make sure you're not doing that.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.