Page 1 of 1

Newb Question - SELECT

Posted: Sat Oct 12, 2002 1:02 pm
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

Posted: Sat Oct 12, 2002 1:13 pm
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 ;)

Re:

Posted: Sat Oct 12, 2002 1:18 pm
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

Posted: Sat Oct 12, 2002 1:33 pm
by volka

Re

Posted: Sat Oct 12, 2002 2:53 pm
by jonat8
Thanks. I figured it out from that sticky about the concerning passing variables in 4.2+ 8)

jonat8