SELECT QUERY using a "STRING" instead of ID

I have been trying to do the following select statement:

SELECT * from <mytable> WHERE <myfield> = ‘$myvar’

$myvar is taken from the URL so: www.mydomain.com/index.php?myvar=example

My problem is I cannot select the data if <myfield> is a text field!!

If <myfield> was an integer, lets say ‘ID’ and I passed the ‘ID’ from the URL, e.g: -

www.mydomain.com/index.php?myvar=3

Then all is fine!!

I think my problem is somehow the string is not recognised correctly, I have even tried to echo the value of the string and it looks exactly as I want it… I have also tried hard-coding the string I want into the query and that works, but not if I use the variable… I am stumped!!

Any words of wisdom?

example:


"SELECT * FROM users WHERE username = '$username'";

That should work for you.

I tried this and no success, this seems perfectly logical though, im gonna go home and try it again – maybe im going mad!

Give you the verdict later.

In the interim, has anyone come across this same problem?

depending on your server’s config. you might have to use superglobal arrays (i.e $_GET[‘myvar’] )…

try printing your SQL before executing to see whats being passed

*edit - my brain is on vacation please leave a message at the sound of the beep.

I assume it’s PHP since he used the dollar-sign variable notation

blink blink

have i been messing with PHP that long that it just seemed normal to have the $ there?

someone wake me up please, i’m having a nightmare

Haha, messing with php is a nightmare? :stuck_out_tongue:


#SELECT * from <mytable> WHERE <myfield> = '$myvar'
#$myvar is taken from the URL so: www.mydomain.com/index.php?myvar=example
# than as Ahmed stated
#youre query should be
$query = "SELECT * from <mytable> WHERE <myfield>='".$_GET['myvar']."'";
$result = mysql_query($query);
echo $query; #debug

#if $_GET['myvar'] is not being returned when its some 
#textfields something else is wrong, show us the part
#that suppose to send that myvar $var :)

Thanks a lot guys!

Digitalosophy my mistake… you were right, Im sure I tried this last night. I must have been up too late and had red-eyes!

Ahmed, out of interest, what do you mean about: -
superglobal arrays (i.e $_GET[‘myvar’] )…

Where would I check my server settings for this, and what would I be looking for?

Im always interested in learning more for the future?

http://uredomein.com?action=something


echo $_GET['action']; #will print out something

NO extra settings needed for this. Only setting which you can change and should change is to turn of globals in php.ini

Than use $_POST[‘submit’] $_POST[‘form_var’] && GET[‘vars’] to fetch them, this way you are sure where they come from. php.net can tell u more .

Enjoy