MySQL Forums
Forum List  »  PHP

Re: how to use PHP variable in MYSQL query?
Posted by: Adam Mochrie
Date: February 07, 2009 09:05AM

Hey guys and gals ... here is a simple way to put any variable into a SQL statement.

Lets say you get information passed via post...

//Set the variable

$var1 = $_POST["user"];
$var2 = $_POST["password"];

// use the '%s' as a place holder and the substitute in the variables.
// You can have as many subs as you want, just make sure you list the variables
// in the order they come in the statement.
$query = sprintf("SELECT * FROM table WHERE user = '%s' AND password = '%s'",
$var1,
$var2);
// Then run the query.
$result = mysql_query($query);

Options: ReplyQuote


Subject
Written By
Posted
Re: how to use PHP variable in MYSQL query?
February 07, 2009 09:05AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.