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

4
  • stop using mysql_*, do it now before you have no choice Commented Nov 6, 2017 at 20:29
  • Are you always only wanting the value of the howdidyoufind key? Commented Nov 6, 2017 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. stackoverflow.com/questions/1258743/normalization-in-mysql Commented Nov 6, 2017 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. :/ Commented Nov 6, 2017 at 20:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.