Reputation: 153
I'm trying to retrieve an email address from a table in MySql using $keyword
(keyword can be anything in the question field) to identify the row. I am successful in finding the row I need with the query below but it returns the entire row, how does one pull just the email out of the row and store it as $email
?
Query:
$result = mysql_query("SELECT * FROM ask WHERE date = '$keyword' order by ask_id")
or die(mysql_error());
Table:
| ask_id | store | fname | lname | email | phone | city| state | zip_code |question | sku | date |
Upvotes: 2
Views: 434
Reputation: 1356
First off, the obligatory mysql_* commands are deprecated, don't use them, kittens will die and your dog will get shot etc. For more on that, please see this question.
If you only want to retrieve one column from your MySQL database you can do so by specifying the column after your SELECT, instead of an asterisk. So you would have a query as follows:
SELECT email FROM ask WHERE date = '$keyword' order by ask_id
You can use this as follows in PHP code:
$result = mysql_query("SELECT email FROM ask WHERE date = '$keyword' order by ask_id")
or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
var_dump($row);
}
To reiterate, you should not be using the mysql_* functions. There are superior replacements available, as detailed in the question referenced above.
Upvotes: 0
Reputation: 27
$result = mysql_query("SELECT columnName AS email FROM ask WHERE date = '" . $keyword . "' ORDER BY ask_id");
while($row = mysql_fetch_assoc($result)){
$row['email']; // Here Do Anything With Email...
}
Upvotes: 0
Reputation: 23490
Just select only the column you need email
instead of them all *
$result = mysql_query("SELECT email FROM ask WHERE date = '$keyword' order by ask_id")
Note that mysql_*
function are deprecated, better to switch to either mysqli
or PDO
. So you will be able to use prepared statements
and you will avoid any risk of mysql injection, learn more here How can I prevent SQL injection in PHP?
Upvotes: 4
Reputation: 86
SELECT `email` FROM ask WHERE date = '$keyword' order by ask_id
Use code above instead. SELECT * FROM...
in your mysql statement means select everything.
Upvotes: 1