I have search a lot but didn’t found the suitable answer for this.
I have used this:
$con=mysql_connect('localhost','root','');
mysql_select_db('db_name',$con);
and I have also used this:
$con=mysql_connect('localhost','root','');
mysql_select_db('db_name');
Both Works For Me..
What is the difference between them and what is pros and cons of these?
Ignoring, for a moment, the whole deprecation thing, the main difference here is in some magic that the
mysql_family pulls off.In specific, all
mysql_family functions that take the link identifier (statement handle, database connection resource, whatever you wish to call it) do so optionally. If the parameter is omitted, it will the last opened connection.This is not the case with the procedural version of
mysqli_, even though many of the functions have the same name and purpose. In fact, the argument order inmysqli_always has the connection first instead of last just for this reason.You should, as best practice, never omit the link identifier in
mysql_. Then again, as best practice, you shouldn’t be usingmysql_. The official deprecation notice went up in the manual today.Methods in PDO and mysqli in object-oriented mode also operate without a connection argument, as the connection is the object on which you call methods.