Newb Question - SELECT

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jonat8
Forum Newbie
Posts: 15
Joined: Sat Oct 12, 2002 1:02 pm

Newb Question - SELECT

Post by jonat8 »

Hi,

I'm new to using PHP and mySQL together.

All the tutorials I've seen on retrieving data from a mySQL database via. PHP just use a fixed criteria, i.e. SELECT * from sometable WHERE somefield = 'fixedvalue';

How can I query the database so that the input of a user in a text box is the criteria, i.e. if the user types in "monkey" for example and presses Submit, the query run would be SELECT * from sometable where somefield = 'monkey';

As I said, I'm new to this. :oops: Preferably the form field and Submit button would be on the same page as the query results.

Thanks
jonat8
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what you pass to mysql_query is simply a string. How this string was built doesn't matter to the function.

Code: Select all

mysql_query("SELECT * from sometable WHERE somefield = 'fixedvalue'");
works as well as

Code: Select all

mysql_query('SELECT' . ' * from sometable ' . 'WHERE somefield = ' . "'fixedvalue'");
or

Code: Select all

$query = "SELECT * from sometable WHERE somefield = '".$var."'";
mysql_query($query);
now make $var the user-input-variable and there you are ;)
jonat8
Forum Newbie
Posts: 15
Joined: Sat Oct 12, 2002 1:02 pm

Re:

Post by jonat8 »

Sorry, I'm being especially dense tonight.

What I need to know is how to tie the form field up with a PHP variable, so that form entry will go into a variable which can then be used as part of the query in the way you described.

Thanks
jonat8
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

jonat8
Forum Newbie
Posts: 15
Joined: Sat Oct 12, 2002 1:02 pm

Re

Post by jonat8 »

Thanks. I figured it out from that sticky about the concerning passing variables in 4.2+ 8)

jonat8
Post Reply