Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

I can'tquite get this select statemen right
Welcome, Guest. Please login or register.
November 21, 2008, 02:47:55 PM
11306 Posts in 1249 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Databases  |  MySQL  |  I can'tquite get this select statemen right « previous next »
Pages: [1]
Author Topic: I can'tquite get this select statemen right  (Read 466 times)
thepreacher

Offline Offline

Posts: 77


« on: March 21, 2007, 09:35:14 AM »

The following is a select statement i am struggling with:
Code
Language: sql (GeSHi-highlighted)
$query = "SELECT '$transtype' FROM chargestable WHERE tramount = '$tramount'";

'$transtype is a variable which can hold values cash or bank or cheque. The values are also the same as their corresponding column names in the table.

When i perform the query, it returns the name of the column instead of the value stored in the column.
I'll appreciate some help please.

Thnx
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #1 on: March 21, 2007, 09:58:49 AM »

Lose the single quote around transtype following the SELECT.

Code:
$query = "SELECT $transtype FROM chargestable WHERE tramount = '$tramount'";
Logged

Rod
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #2 on: March 21, 2007, 10:04:20 AM »

single quote is the string delimitor in SQL (as is in Pascal or C)
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
thepreacher

Offline Offline

Posts: 77


« Reply #3 on: March 21, 2007, 10:25:32 AM »

Am confused ... but its acceptable around the variable after the = symbol?
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #4 on: March 21, 2007, 10:31:26 AM »

$query = "SELECT '$transtype' FROM chargestable WHERE tramount = '$tramount'";

is the same as

SELECT 'ROD' FROM chargestable WHERE  tramount = '$tramount'";

It's always going to return 'ROD'

Logged

Rod
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #5 on: March 21, 2007, 11:40:44 AM »

no need for confusion preacher ;-)
rod said it, I will add one more paint layer
is a set of characters is enclosed in single quotes, it is a string, ie a LITERAL value. It has no other meaning than a string.

select 1+2; [from dual if you're on Oracle-the-clumsy ;-)] returns 3
select '1+2'; returns '1+2'


got it ?

in your case, the column names following the SELECT and preceding the FROM have to be column names (to extract their value from the rows, ok ?) and NOT literals (or they're returned "as is", as literals)

ok ?

one case (it's the case using this word twice ;-) where literals are used in query phrases (SELECT) is for example when you've numerical or boolean values you want to "translate" into human-readable form (so that your front-end, usually PHP, doesn't have to do this himself), like in (typos possible)

select a.firstfield, substr(a.secondfield,1,4) AS extract_1_4, `date`, case when a.booleanfield then 'true' else 'false' end, b.somefield from firsttablename AS a, secondtablename AS b WHERE...

in this exampe, you'll see string literals used (single quotes), fields/columns having bad names (reserved SQL words or why not spaces or non-printable characters...) being enclosed in backticks (`), aliases for columns and tables (using AS, good practice IMHO) and even a CASE WHEN which is nice.
try this on one of your tables' data you'll understand what I mean.
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: [1]
« previous next »
    Jump to: