-2

i have function connect()

function connect(){
    $host = '';
    $name = '';
    $pass = '';
    $port = '';
    $db = '';
    $con = mysqli_connect($host,$name,$pass,$db,$port) or die (mysqli_connect_error());
    return $con;
}

And i have function check_exists

function check_exists($username){
    $num = mysqli_num_rows(mysqli_query($con,"SELECT * FROM `accounts` WHERE `user` = '$username'"));
    return $num;
}

but check_exists don`t know what variable $con is. How can i make that check_exists will know what $con is?

JJJ
  • 32,902
  • 20
  • 89
  • 102
UareBugged
  • 176
  • 1
  • 2
  • 11
  • Well essentially, every time you call the `connect` function, you'll get a new `$con` object. `mysqli_num_rows(mysqli_query(connect(),"SELECT...` However it might be better to call this function once and store the connection object outside of the function scope so that more functions can use it. – Lix Mar 15 '15 at 08:46

2 Answers2

4

you can pass the $con to the function like

 $con =  connect();
 check_exists($username,$con);

function check_exists($username,$con){
        $num = mysqli_num_rows(mysqli_query($con,"SELECT * FROM `accounts` WHERE `user` = '$username'"));
        return $num;
    }
Kamran
  • 2,711
  • 2
  • 17
  • 24
-1

Well for one you could make the $conn variable globic or you can get it but calling the connect function $conn = $this.connect(); in your check_username method

nniicc
  • 236
  • 2
  • 13