6

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']);
Deepak
  • 6,684
  • 18
  • 69
  • 121
nix
  • 3,262
  • 2
  • 21
  • 36

1 Answers1

7

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

Deepak
  • 6,684
  • 18
  • 69
  • 121
  • So you mean that my $this->_Con will be the same with these 2 method?? But when i try to use the second method, i guess my insert statement fails to insert new row. Do you have any idea why?? – nix Oct 04 '12 at 03:34
  • 1
    @user1650716 Generally you use *either* the object from `new mysqli()` *or* the structure from `mysql_init()`. Not both. – staticsan Oct 04 '12 at 04:12
  • i cant run prepare query.. it says: Call to a member function prepare() on a non-object.. can you please show me an example on how can i run insert query with prepared statements?? – nix Oct 04 '12 at 06:15
  • 1
    or my true goal is to set MYSQLI_CLIENT_FOUND_ROWS from affected rows to matched rows when running update statements, is there a way to achieve this without using real_connect?? – nix Oct 04 '12 at 06:22
  • @user1650716 can you post how you are using ur prepared statements ? update your question!! – Deepak Oct 04 '12 at 09:44