0

For some reason I drawing a complete blank on how to display information im looking for from this array. This is the result of this query...

$res = mysql_query("SELECT `key`, `value` FROM `data` where `id` = '4534'", $db_connection);
while ($row = mysql_fetch_assoc($res))
{
    print_r($row);
}

result comes out like this

Array ( [key] => am3:id [value] => 5198 ) 
Array ( [key] => dob [value] => 1984-11-15 ) 
Array ( [key] => examdate [value] => 1 ) 
Array ( [key] => howdidyoufind [value] => Facebook )

if I need to place for instance the value of "howdidyoufind" into a variable how would I do that? so the value of the variable would be "Facebook".

Any help would be appreciated, thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612
robby
  • 23
  • 6
  • stop using `mysql_*`, do it now before you have no choice –  Nov 06 '17 at 20:29
  • Are you always only wanting the value of the `howdidyoufind` key? – Patrick Q Nov 06 '17 at 20:30
  • You should really look into database normalization as you are going to run into issues down the line if you move forward in this way. Here is another SO question and answer that should help you with that. https://stackoverflow.com/questions/1258743/normalization-in-mysql – cmorrissey Nov 06 '17 at 20:36
  • Noted. No i need to pull multiple Key values, there are about 20 in the full result. The database is setup by a 3rd party application we use for member management, it has to be set up the way it is for it to work. :/ – robby Nov 06 '17 at 20:42

1 Answers1

5

Use an if statement:

if ($row['key'] == 'howdidyoufind') {
    $variable = $row['value'];
}

You could also do it in the SQL:

SELECT value
FROM data
WHERE id = 4534 AND key = 'howdidyoufind'

Then the value you want will be in the only row returned by the query.

Barmar
  • 741,623
  • 53
  • 500
  • 612