-2

I'm currently switching from mysql to mysqli and I'm getting errors when I'm converting this function which was working under mysql.

include("config.php");
function getUsername($uid){
    $sql = "SELECT username FROM users WHERE id='".$uid."' LIMIT 1";
    $result = mysqli_query($conn,$sql) or die(mysqli_error(""));
    $row = mysqli_fetch_assoc($result);
    return $row['username'];
}

Errors:

Notice: Undefined variable: conn

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in

Warning: mysqli_error() expects parameter 1 to be mysqli, string given in

How should it be written?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Johnny
  • 63
  • 1
  • 7
  • possible duplicate of [Notice: Undefined variable: conn](http://stackoverflow.com/questions/23699994/notice-undefined-variable-conn) – mario Jun 23 '15 at 18:33
  • You have a history of low-research and regularily downvoted questions and might lose your question-asking privileges. [You should read this before you post your next one.](http://meta.stackoverflow.com/questions/254262/before-you-post-your-next-question) – mario Jun 23 '15 at 18:38

1 Answers1

-1

Problem Solved.

Added

global $conn; 
inside the function.
Johnny
  • 63
  • 1
  • 7