Closed Thread Icon

Topic awaiting preservation: MySQL/PHP - pass variable to a MySQL select statement? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12354" title="Pages that link to Topic awaiting preservation: MySQL/PHP - pass variable to a MySQL select statement? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: MySQL/PHP - pass variable to a MySQL select statement? <span class="small">(Page 1 of 1)</span>\

 
DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 08-08-2002 01:59

Been playing around with a few things and can't figure out if I'm doing something wrong or if I simply can't do it (this is all very new to me).

I want to specify a variable via the URL.

I want to query the database looking for results where a specified field matches that variable.

Can I do this?

If so, what is the proper syntax?

Any help appreciated. Thanks =)



Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-08-2002 02:03

As in
"select * from table where name = '$name'";

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 08-08-2002 02:30

Well, yes.

But it's giving me problems.

I have my query setup the way it's gone through in the GN tutorial -

$query = "select * from brewery_tbl where brewery_country=$country order by brewery_name";

$result = mysql_query($query, $connection);

while ($row = mysql_fetch_row($result))

..yadda yadda.

I get this error on the page -
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource

I have the country specified in the URL, and there are results that match (and the table/column names are valid).

If I take out the 'where' clause, and just get all the results, it works fine.

Do I have something seriously wrong? Have I overlooked something silly?





bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 08-08-2002 03:38

When you are using strings you usually need to surround the passed info with single quotes (the exception is for numbers but it's best just to always use them)
$query = "select * from brewery_tbl where brewery_country= '$country' order by brewery_name";

after the query put:

echo mysql_error() to see the error message.



.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 08-08-2002).]

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 08-08-2002 03:55

Ok, well I had been playing around with and without quotes, and with quotes I get no warning, but get no results either.

I tried echoing $result, and got this - "Resource ID #2".

I have no idea what that means....?

Trying to echo mysql_error() has given no result either

I guess I need to go read some more and try again

Thanks.

{{edit -

ok, I feel like an ass. It seems to be a matter of case-sensitivity. I had *thought* I had read that it would not be case sensitive for this issue, but it is. I also thought I had covered myself by trying it both ways, but didn't realize the URL resolved to a lowercase letter either way. The brewery_country data all starts with a capital letter. The URL was passing lowercase, even if I typed it as Uppercase. So, now I just need to find how to pass an uppercase first letter via the URL or make the search case-insensitive.

}}




[This message has been edited by DL-44 (edited 08-08-2002).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-08-2002 04:16

Well, I don't have time to look at the tutorial right now, but I use mysql_fetch_array more than mysql_fetch_row. It's a little easier to work with IMHO.

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 08-08-2002 04:30

Yeah? I've been looking at both of them, but haven't gotten as far as trying out mysql_fetch_array yet.
I seem to have a solution, whether it's the best one I can't say, but it works. Just a simple $country = ucfirst($country); before the query has done the trick.

We'll see how the rest of it goes now.

Thanks again =)


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 08-08-2002 06:04

Glad you got it working....

Did you get my email about the new Gurus forums?

DL-44
Maniac (V) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 08-08-2002 14:39

Yes. I have a couple mails I have to respond to still - I'll get to yours as soon as I get home tonight. I haven't gone back to the styling work for the forum since the first bout, have to talk to emperor about a couple technical issues and then I'll be a bit more clear on all of it



« BackwardsOnwards »

Show Forum Drop Down Menu