# SELECT QUERY using a "STRING" instead of ID

**URL:** <https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676>\
**Category:** Uncategorized\
**Created:** [August 17, 2004, 9:14am UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676 "2004-08-17T09:14:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![w\_digital](https://avatars.discourse-cdn.com/v4/letter/w/35a633/32.png) [@w\_digital](https://forum.kirupa.com/u/w_digital)\
**Post date:** [August 17, 2004, 9:14am UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/1 "2004-08-17T09:14:54Z")

</div>

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](http://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](http://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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 17, 2004, 1:01pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/2 "2004-08-17T13:01:37Z")

</div>

example:

```auto

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

```

That should work for you.

---

<div class="post-metadata">

**Author:** ![w\_digital](https://avatars.discourse-cdn.com/v4/letter/w/35a633/32.png) [@w\_digital](https://forum.kirupa.com/u/w_digital)\
**Post date:** [August 17, 2004, 2:45pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/3 "2004-08-17T14:45:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://forum.kirupa.com/u/ahmed)\
**Post date:** [August 17, 2004, 2:47pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/4 "2004-08-17T14:47:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 17, 2004, 2:52pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/5 "2004-08-17T14:52:53Z")

</div>

try printing your SQL before executing to see whats being passed

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 17, 2004, 3:41pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/6 "2004-08-17T15:41:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://forum.kirupa.com/u/ahmed)\
**Post date:** [August 17, 2004, 4:02pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/7 "2004-08-17T16:02:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 17, 2004, 4:07pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/8 "2004-08-17T16:07:09Z")

</div>

_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

---

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://forum.kirupa.com/u/ahmed)\
**Post date:** [August 17, 2004, 4:18pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/9 "2004-08-17T16:18:33Z")

</div>

Haha, messing with php is a nightmare? 😛

---

<div class="post-metadata">

**Author:** ![ol4pr0](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@ol4pr0](https://forum.kirupa.com/u/ol4pr0)\
**Post date:** [August 17, 2004, 7:07pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/10 "2004-08-17T19:07:28Z")

</div>

```php

#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 :)

```

---

<div class="post-metadata">

**Author:** ![w\_digital](https://avatars.discourse-cdn.com/v4/letter/w/35a633/32.png) [@w\_digital](https://forum.kirupa.com/u/w_digital)\
**Post date:** [August 17, 2004, 8:11pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/11 "2004-08-17T20:11:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ol4pr0](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@ol4pr0](https://forum.kirupa.com/u/ol4pr0)\
**Post date:** [August 17, 2004, 8:22pm UTC](https://forum.kirupa.com/t/select-query-using-a-string-instead-of-id/119676/12 "2004-08-17T20:22:00Z")

</div>

[http://uredomein.com?action=something](http://uredomein.com?action=something)

```php

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
