Reputation: 24426
(Beginners question here...be gentle)
My php script is a response to a form. Assuming I have already connected to a database and extracted the post variables (just two - emailAddress and username), like so
mysql_connect('localhost', 'dbusername', 'dbpassword');
mysql_select_db('database');
extract($_REQUEST);
..and knowing that within the 'users' table, there are 'emailAddress', 'username' and 'address' fields. (The email addresses will all be unique..)
How would I search thru the table to get at the specific 'address' after receiving the emailAddress and username (and output something else if there is no name and address which matches)?
seems absurdly difficult to get an answer. this must be a very very very very very very difficult question.
Extra-special thanks to users who told me the problem was very easy, and yet couldn't come up with an answer. I salute your indefatigability, your magnificent courage, and your willingness to help.
A possible answer to my question is:
$result = mysql_query("SELECT * FROM users WHERE emailAddress='".$emailAddress."' AND username='".$username."'");
$row = mysql_fetch_array($result);
if ($row['username'] == "") {
// no results
} else if ($row['emailAddress'] == $emailAddress AND $row['username'] == $username) {
// found result
echo "The address is ".$row['address'];
} else {
// i guess something else happened
}
Be sure to tell me how this is wrong, and the real answer is easy, and yet not come up with an answer.
Upvotes: 0
Views: 92
Reputation: 11
you have to take this sql request into a variable then execute it after that u can verify if the array is empty or not.. if it is it means that there are no combination with both that name and that email corresponding into your database.. and it's done!`$req= "Select name,address from users where emailaddress=$email and name=$name"; $result = mysql_query($req); if !isset($result){ /// some stuffs there when everything okkay
} else{ /// some stuffs if there is no record corresponding
}` in vrac.. some sample exemple in the idea.. note that the sql query give the result into an array so u have to manipulate an array or hash
Upvotes: 1
Reputation: 1794
to get individual elements, you do "select column_name from table where criteria".
in your example it would be
select address from users where emailaddress = '[email protected]' and name = 'John Doe'
the column_name section can be multiple items comma seperated, so, for example, if you wanted name and address, you would do
select name, address from users where emailaddress = '[email protected]' and name = 'John Doe'
Upvotes: 1
Reputation: 190945
You can use a SELECT
statement. Note this code is not safe to SQL injection.
SELECT * FROM Users WHERE emailAddress = '[email protected]'
Upvotes: 0