annoying syntax problem (i think)

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

malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

annoying syntax problem (i think)

Post by malcolmboston »

heres the code
basically it wasnt working, so i thought id print out the query it is trying to do, it gives out this:
SELECT password from ".$tablename." WHERE username = ''
not sure why it is doing that at all, put thats all i can think of this problem and i have no idea how to fix it does anyone have any ideas?

Code: Select all

<?php 
// database variables for connections 
$host = "localhost"; 
$user = "malcolmboston"; 
$password = "xxxxx"; 
$DBname = "TDN"; 
$tablename = "login_DB"; 
//connection variables completeed 

// establishing connections 
$link = mysql_connect ($host, $user, $password); 
//connection established 

//the query defined 
$query = "SELECT password from ".$tablename." WHERE username = '".$_SESSION['username']."'";

//the result 
$result =mysql_db_query($DBname, $query, $link); 
$line = mysql_fetch_array($query, MYSQL_ASSOC); 

echo $result;
echo $line['username'];
?>
the session variable is getting set because i can echo it on any page using

Code: Select all

print $_SESSION['username'];
does anyone have any insight into teh problem?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

are you 100% sure that when you echoed the query you got

Code: Select all

SELECT password from ".$tablename." WHERE username = ''
Can't see why is would parse one variable and not the other, your code is technically correct, i ran it on my server and works fine.

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

exactly!

neither can i

not sure if the end digits where '' (2 commas) or "(quotation mark) but yeah thats what it said, absolutely baffling

and im gonna stress it again the variable is definitely there!!! i echo it on every page and it gives me back a value of malcolmboston, test, admin or whoever i decide to login as

this is really doing my head in
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

btw i use

foxserv3.o php/mysql/apache model
on a windows 2000 pro box
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what if you just do

Code: Select all

echo $tablename;
Does it print login_DB to the screen?

Mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is that the whole page? Do you do a session_start() on that page? Is this really echoed as shown ' ".$tablename." '? Cause that would be really weird.

BTW, [php_man]mysql_db_query[/php_man]() is a deprecated function.

Mac
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

using the table variable works perfectly but i changed it to login_DB when i was debugging, it worked exactly the same either way

and yes a session start is called because it can display my session var seperately on the page when i echo it

i also have lots of other coding written that
echos session name etc

its all there, thats what i dont understand
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

no1 got any ideas about a possible cause to this problem?

ive viewed it in zend and DW both have problems displaying correct result, zend says its correct but still wont give me the result and tells me nothing about the error
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

so can you echo $tablename or not?

If you can, and judging by previous problems you have had, you PHP installation seems to be f*cked.

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

yep
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ive just had an idea but cannot test it until i get home

if at the beginning of the page i put something to the effect of

Code: Select all

?php
$_SESSION['username'] = $value;
and then in the query put

Code: Select all

$query = "SELECT password from ".$tablename." WHERE username = ".$value."";
then would that work?

because that would make it alot simpler query-wise and turn it more like $tablename which works

possibly pointless as it is doing the same thing but simplifies the query so it may be able to read it
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

shouldn't make any difference whatsoever.

I would be worried that you can echo $tablename in one place, but can't concatenate in another.

That is just weird!

As i said before, i think your PHP installation may well be f*cked

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

no it cant be

because i can echo it anywhere, but the MYSQL query wont do it

the var is there, but mysql wont include it in its query
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

it has nothing to do with MySQL, it is still PHP where it can't include it.

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

well, i cannot see how it is a PHP error when it is using it in other circumstance

when i create the variable it there because i can retrieve it anywhere on the site!!
(this is how i get hello %username% to work always and without problems)

its the query that is going wrong
Post Reply