2

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 |

Carrie Kendall
  • 10,761
  • 5
  • 57
  • 79
Scott Morrison
  • 153
  • 1
  • 8

4 Answers4

4

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?

Community
  • 1
  • 1
Fabio
  • 21,516
  • 12
  • 49
  • 63
  • I tried this and when I echo it to see what I get it says "Resource #4" instead of the email, if there a further step I need to take? $email = $result; echo " $email" – Scott Morrison Jun 14 '13 at 12:45
  • $email = $result; echo " $email"; – Scott Morrison Jun 14 '13 at 12:47
  • @Scott try to fetch an array first and then echo it `$row = mysql_fetch_array($result); echo $row['email'];` – Fabio Jun 14 '13 at 12:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31765/discussion-between-fabio-and-scott-morrison) – Fabio Jun 14 '13 at 12:55
1
SELECT `email` FROM ask WHERE date = '$keyword' order by ask_id

Use code above instead. SELECT * FROM... in your mysql statement means select everything.

Joe
  • 86
  • 1
  • 5
0

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.

Community
  • 1
  • 1
WouterH
  • 1,291
  • 10
  • 16
0

$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...
}

  • I tried that and get this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /var/www/html/ask/admin_search/functions_search.php on line 20 – Scott Morrison Jun 14 '13 at 13:03
  • @ScottMorrison AS assigns the data to column "Email" in your MySQL return set. It's not necessary in your example, as it just renames the column. Your problem appears to be to get the data from your MySQL result into a PHP variable. I recommend picking up some documentation on the technologies you intend on using and reading it. – WouterH Jun 14 '13 at 13:04
  • Thanks WouterH I am with you on that, I've been reading this http://www.amazon.com/PHP-MySQL-Web-Development-4th/dp/0672329166/ref=sr_1_2?s=books&ie=UTF8&qid=1371215343&sr=1-2 which has been great, but I have this very specific thing I'd like to accomplish today. I'm a noob to a lot of this. I appreciate everyone's help. – Scott Morrison Jun 14 '13 at 13:10
  • @ScottMorrison Alright, good luck with the reading :) Please see my updated answer for your specific case. – WouterH Jun 14 '13 at 13:45