Answer the question
In order to leave comments, you need to log in
Why doesn't the function work?
The code works fine "outside the function".
$select_result=mysql_query("SELECT `api_key` FROM `users` WHERE `id`='$all_about_user_array[id]' LIMIT 1");
$row=mysql_fetch_assoc($select_result);
$api_key=$row['api_key'];
echo"$api_key";
function user_api_key()
{
$select_result=mysql_query("SELECT `api_key` FROM `users` WHERE `id`='$all_about_user_array[id]' LIMIT 1");
$row=mysql_fetch_assoc($select_result);
$api_key=$row['api_key'];
return $api_key;
}
echo user_api_key();
Answer the question
In order to leave comments, you need to log in
$all_about_user_array[id] is
lost
and in general your code does not work very well.
in the yard in 2018.
mysql_* functions have been deprecated since version 5.5 (which is over 4 years old)
and what is the id? constant?
$all_about_user_array['id'] where does it come from in the function? Need to transfer
function user_api_key($user_id)
{
$select_result=mysql_query("SELECT `api_key` FROM `users` WHERE `id`='$user_id' LIMIT 1");
$row=mysql_fetch_assoc($select_result);
$api_key=$row['api_key'];
return $api_key;
}
echo user_api_key($all_about_user_array['id']);
Please explain what exactly is not working. Doesn't display api_key?
Instead of echo do this
What did the output give?
1. learn to debug.
if something does not work, you need to check everything step by step.
which is passed to each function.
what is returned from each function.
2. stop using mysql_, switch to mysqli_ / pdo
3. update the PHP version to the current
PS
answer to the question - because $all_about_user_array['id'] is not defined inside the function.
it must be passed there with the
PPS argument
Instead
write at least
mysql_query('SELECT `api_key` FROM `users` WHERE `id` = ' . (int)$all_about_user_array[id] . ' LIMIT 1');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question