php - Pass object as parameter in function not working -


this seems extremely easy life of me can't figure out why not working. trying pass object function error:

catchable fatal error: argument 3 passed write_query() must instance of mysqli, null given, called in c:\xampp\htdocs\csp\login\login.php on line 28 , defined in c:\xampp\htdocs\csp\php_scripts\login_script.php on line 40

these functions definitions:

function conn_database()     {         if (!empty($_post['userid']) && !empty($_post['password']))         {             $conn = new mysqli('localhost', 'root', '', 'csp');              if (mysqli_connect_errno())              {                 echo 'connection database failed:'.mysqli_connect_error();                 exit();             }             return $conn;         }      }   function write_query($userid, $password, mysqli $conn)     {         $query = 'select * authorized_users '         ."where userid= '$userid'"         ."and password_id='$password'";         $result = $conn->query($query);         return $result;     } 

i calling them this:

$conn = conn_database(); $result = write_query($userid, $password, $conn); 

i'm type hinting in definition of write_query() understand error, third argument not considered instance of mysqli. (i aware should salting password , all, i'm trying work first). suggestions appreciated

your third paramater not instance of mysqli. null. because get_connection function not returning anything.

why checking this:

if (!empty($_post['userid']) && !empty($_post['password'])) 

this means code try , connect sql server if provide post data userid , password only. if behaviour want, should put if statement outside function, this:

if (!empty($_post['userid']) && !empty($_post['password']))   $conn = conn_database();   $result = write_query($userid, $password, $conn); } 

although seems userid , password aren't set, regardless. make sure check name attribute on inputs , var_dump($_post) see whats being posted script.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -