oop - How to import database connection class in another class in php -


i going import databaseconnector class in controller class. databaseconnector class contains database connection queries. both classes different php file.

here code.

databaseconnector:

class databaseconnector {     public $con;     //function getting connected database     public function getconnection()     {         $con=mysqli_connect("localhost","root","","abcd");         // check connection         if (mysqli_connect_errno()) {           echo "failed connect mysql: " . mysqli_connect_error();         }         return $con;     }     public function closeconnection()     {         mysqli_close($con);     } } 

controller:

include "database_connector.php";     $dc = new databaseconnector();     $con = $dc->getconnection(); class controller {      public function __construct(){      }      public function insertdata()     {          $name = "abcde";         $sql = "insert user_details values('".$name."')";         mysql_query($con, $sql);     }   } 

where wrong in code. shows me error on mysql_query($con,$sql): $con in undefined variable; , mysql_query() requires 2 parameters

please me.

your $con not in class.

include "database_connector.php"; class controller {     protected $con;      public function __construct(){         $dc = new databaseconnector();         $this->con = $dc->getconnection();     }      public function insertdata()     {         $name = "abcde";         $sql = "insert user_details values('".$name."')";         mysqli_query($this->con, $sql);     } } 

i don't test code, should work.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -