I am self learning php and trying oop, I am struggling with following problem, Would anyone could help me how can I use following database connection in to another class function. In php.net it is defined as $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); but when I use this within a class it dose not work. Thanks
class dbconnect{
private $host;
private $user;
private $pass;
private $dabase;
function doConnect()
{
$this->host = 'localhost';
$this->user = 'root';
$this->pass = 'abc@123';
$this->dabase = 'database_5';
$db = new mysqli($this->host, $this->user, $this->pass, $this->dabase);
if (mysqli_connect_errno())
{
echo "<br /><hr />";
echo "<p style='align:center;'>Error : could not connect to database.. </p>";
echo "<hr />";
exit;
}
}
$mysql = new dbconnect();
function doQuery($mysql){
$queryUser = $mysql->query("SELECT * FROM b_admin_user WHERE username_d = 'admin'");
echo $queryUser_row = $queryUser->num_rows;
}
doQuery($mysql);
$db
insidedoConnect()
, and call it in the constructor, or call$mysql = $mysql->doConnect()
– 1nflktd Jul 6 at 13:25mysqli_connect_errno()
is procedural style. Check$db->connect_errno()
. – GolezTrol Jul 6 at 13:46