What is the difference between mysqli::real_connect and new mysqli object in connecting database?

我只是一个虾纸丫 提交于 2020-12-08 07:14:49

问题


I'm using this method to connect to mysql db :

$this->_Con = new mysqli($this->_DB['Server'],$this->_DB['User'],$this->_DB['Pass'],$this->_DB['DB']);

What is the difference when I connect using this method:

$this->_Con = mysqli_init();
$this->_Con->real_connect($this->_DB['Server'],$this->_DB['User'],$this->_DB['Pass'],$this->_DB['DB']);

回答1:


Its just another way of connecting to your database. If you use mysqli_init() it will initialize the mysqli object first. Then using the object you will call real_connect() to connect to the database. But when you use new mysqli() you are initializing the mysqli object by passing in the connection values as parameters, So it does initialization and connection at the same time.

Note: Calling the constructor with no parameters is the same as calling mysqli_init().

In your first case you are getting the connection object as the return value because you are calling mysqli() constructor with parameters. So you can run queries on it. But in your second case you need to store the connection like this,

$con = $this->_Con->real_connect($this->_DB['Server'],$this->_DB['User'],$this->_DB['Pass'],$this->_DB['DB']);

Then run queries on $con



来源:https://stackoverflow.com/questions/12719826/what-is-the-difference-between-mysqlireal-connect-and-new-mysqli-object-in-con

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!