E
E
Evgeny Ivanov2018-02-09 09:21:19
PHP
Evgeny Ivanov, 2018-02-09 09:21:19

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";

But designed as a function - no.
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();

Why doesn't the function work?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stanislav B, 2018-02-09
@logpol32

$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?

R
riot26, 2018-02-09
@riot26

$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']);

PS Mysql-extension for working with the database is outdated and cannot be used. Read more in the red box here

A
Anton, 2018-02-09
@karminski

Please explain what exactly is not working. Doesn't display api_key?
Instead of echo do this
What did the output give?

D
Dmitry Entelis, 2018-02-09
@DmitriyEntelis

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 question

Ask a Question

731 491 924 answers to any question