Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_connect vs mysqli_real_connect

Tags:

php

mysqli

I read the doumentation But I don't understand the difference exactly.

What difference does the connection object make in this? I didn't find any posts.

I misunderstood it. Flags are not present in both. Why didn't they add flags as part of mysqli_connect? Any specific reasons? Which one should I use?

like image 224
Gibbs Avatar asked Jul 21 '15 14:07

Gibbs


1 Answers

mysqli_real_connect() and mysqli_connect is different in way that

mysqli_real_connect() accepts a lot more options than mysqli_connect

For example I am building a health-check script for my Loadbalancer and I want set the very low connection timeout.

Now the connection timeout has to be set using:

mysqli_options() with the option name MYSQLI_OPT_CONNECT_TIMEOUT

Now the thing with mysqli_options() is that this should be called after mysqli_init() and before mysqli_real_connect().

mysqli_connect cannot be used for this purpose.

Hope this explanation helps.

<?php
//create the object
$connection = mysqli_init();

//specify the connection timeout
$connection->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3);

//specify the read timeout
$connection->options(MYSQLI_OPT_READ_TIMEOUT, 3);

//initiate the connection to the server, using both previously specified timeouts
$connection->real_connect('server', 'user', 'pass', 'database');
?>
like image 71
Erfan Ilyas Avatar answered Oct 06 '22 05:10

Erfan Ilyas