I have noticed something while testing different MySQL connection methods in PHP.
I usually use odbc_connect() when making a connection to a database in PHP. I save the resouce variable that odbc_connect() returns into a global constant (using the define() function) that I can conveniently access throughout my application. Like so:
Using odbc_connect() and saving the return value into a constant works fine:
<?php
define("_conn", odbc_connect("Driver={MySQL ODBC 5.3 Unicode Driver};Database=MyDB;", "user", "pass"));
?>
Saving the return value of mysql_connect() (the deprecated one) into a constant also works fine:
<?php
define("_conn", mysql_connect("localhost", "user", "pass", "MyDB"));
?>
However, trying to save the return value of mysqli_connect() into a constant does NOT work:
<?php
define("_conn", mysqli_connect("localhost", "user", "pass", "MyDB"));
?>
Warning: Constants may only evaluate to scalar values in C:\...\script.php on line 164
This is unfortunate, as it would be nice to use mysqli_connect() to establish a connection and save the handle into a constant, where odbc_connect() isn't available. I did research and found that the only two database connection functions that I can use with MySQL that return a RESOUCE (and can be used with the define() function) are odbc_connect() and mysql_connect() (the deprecated one). See this link: http://php.net/manual/en/resource.php
Is there a way to get mysqli_connect() to return a RESOUCE, so that I can use its return value in a constant (using the define() function)?
PDO does not return a RESOUCE either.