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?
system
August 17, 2004, 1:01pm
2
example:
"SELECT * FROM users WHERE username = '$username'";
That should work for you.
system
August 17, 2004, 2:45pm
3
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?
system
August 17, 2004, 2:47pm
4
depending on your server’s config. you might have to use superglobal arrays (i.e $_GET[‘myvar’] )…
system
August 17, 2004, 2:52pm
5
try printing your SQL before executing to see whats being passed
system
August 17, 2004, 3:41pm
6
*edit - my brain is on vacation please leave a message at the sound of the beep.
system
August 17, 2004, 4:02pm
7
I assume it’s PHP since he used the dollar-sign variable notation
system
August 17, 2004, 4:07pm
8
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
system
August 17, 2004, 4:18pm
9
Haha, messing with php is a nightmare?
system
August 17, 2004, 7:07pm
10
#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 :)
system
August 17, 2004, 8:11pm
11
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?
system
August 17, 2004, 8:22pm
12
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