-1

My php script is woking outside function but not inside function.

$uid = $_SESSION['user_id'];
function getUserName($conn) {
$sql = "SELECT * FROM `tbl_user` WHERE `user_id` ='".$uid."' ";
$row = $conn->query($sql)->fetch_array();
return $row["user_name"];
}

Unable to get it where I am wrong.

  • What is the error you are getting? and if you are writing function inside function then it will now work. – urfusion Jul 05 '17 at 07:00
  • What exactly do you mean by outside/inside? – Oasa Jul 05 '17 at 07:00
  • is there actually any errors? – NID Jul 05 '17 at 07:01
  • 3
    Why you are fetching `$uid` from the global scope, rather than passing it as argument to the function? Why are you injecting raw input into your SQL code, rather than using prepared statements? Is your database library configured to throw exceptions? Is your PHP installation configured to display errors? – Álvaro González Jul 05 '17 at 07:02
  • $uid also you need to pass to the function...`function getUserName($conn,$uid) { // }` – lalithkumar Jul 05 '17 at 07:03
  • Why is this being voted as close? This is obviously someone who has trouble with variable scope and doesn't have an environment that displays errors. Are we now just closing everything that is too simple for us? This community is getting less helpful, and full of narcissistic users and condescension. Tell him with about SQL injection, explain about globals en help this guy... – Timmetje Jul 05 '17 at 07:14
  • @Timmetje Stack Overflow is a community driven knowledge base. Dropping some code with "guess why I'm not happy with it" is not a contribution to that. Why can't I edit Wikipedia articles to add questions? That would definitively make Wikipedia more useful for *me*! – Álvaro González Jul 05 '17 at 09:49
  • @ÁlvaroGonzález It seems more like a legit question from a beginner who has a specific programming problem, with a ,minimal, complete, and verifiable example. Good enough according to the help center. StackOverflow is the right place to ask these questions. If there are duplicates because it's already in our "knowledge base" then link and close. But this question is being treated condescending because his question isn't valuable enough for a "knowledge base" full of hip and trendy questions. Why is he using global and unsafe queries, or error reporting because he obviously doesn't know... – Timmetje Jul 05 '17 at 11:35
  • @Timmetje Perhaps you don't have enough rep to see the question history but he was asked for further details ("what error are you getting", "what do you mean exactly by outside/inside"). He decided to ignore that and, instead, edited the question to actually introduce an error that wasn't in the original question (remove `global $uid;` from within function), then he accepted an answer that fixes such typo. – Álvaro González Jul 05 '17 at 11:54

4 Answers4

1

DO NOT use global variable. It's evil.

Pass the user ID as parameter to the function:

function getUserName($conn, $uid) {
    $sql = "SELECT * FROM `tbl_user` WHERE `user_id` ='".$uid."' ";
    $row = $conn->query($sql)->fetch_array();
    return $row["user_name"];
}

echo getUserName($conn, $_SESSION['user_id']);
Chris Lam
  • 3,526
  • 13
  • 10
0

Best thing you can do is avoid globals and just pass the uid as a parameter:

$uid = $_SESSION['user_id'];

$username = getUserName($conn, $uid);

function getUserName($uid, $conn) {
    $sql = "SELECT * FROM `tbl_user` WHERE `user_id` ='".$uid."' ";
    $row = $conn->query($sql)->fetch_array();
    return $row["user_name"];
}

Read http://php.net/manual/en/language.variables.scope.php to learn about variable scope in PHP

Also read this post about SQL injection

Timmetje
  • 7,641
  • 18
  • 36
0

you need to call the function to work code inside funtion

Jayesh Paunikar
  • 148
  • 1
  • 7
0

try this

function getUserName($uid, $conn) {
 // code here
}
Nitish Kumar Diwakar
  • 663
  • 4
  • 14
  • 25