-5

I have migrated from mysql functions in PHP to mysqli functions (from PHP 5.4 to PHP 5.5). The problem is that most of the conversion demands that I use global because I had function like this:

function insert_custom_data($params, $table_name){
    global $conn;
  // do the mysql insert
  $id = mysqli_insert_id($conn);
  // ... rest of the code
}

Is there a possibility of having it working without using global or parameter in the function? maybe with a class?

Cesar
  • 514
  • 1
  • 5
  • 16
  • Take a look: http://programmers.stackexchange.com/questions/148108/why-is-global-state-so-evil – Kevin Kopf Mar 04 '16 at 13:09
  • Possible duplicate of [mysql\_fetch\_array()/mysql\_fetch\_assoc()/mysql\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-expects-parameter-1-to) – Machavity Mar 04 '16 at 13:12
  • That does not solve the question. I don't like globals, I don't use globals. In the comments you refer to there are many examples of disavendtages of the global use. But still I get the problem, how do I solve it? In deprecated mysql functions I didn't have the problem as there was no need to pass the database connection object as parameter. Must I encapsulate all these functions into a class and use the object methodology if I want to use mysqli ? – Cesar Mar 04 '16 at 13:14
  • 1
    If you do not like globals, do not use them and pass the connection as parameter. – Matteo Tassinari Mar 04 '16 at 13:17
  • You are the only one understanding the question. I was not talking about the error, I know why the error happens. What I don't see is the mechanics. I don't like global, and passing the connection as a parameter seems ridiculous to me, there is something missing that I don't get when using mysqli, and yes, I have read the manual, and all the examples have the connection object immediately available, they are not real world examples integrated in hundreds-of-functions projects. – Cesar Mar 04 '16 at 13:19
  • What do a `var_dump($conn)` at the start of the function and before the last_insert_id say? – apokryfos Mar 04 '16 at 13:22
  • "passing the connection as a parameter seems ridiculous to me". Why writing functions does not seem ridiculous to you? You could write all the operations in global scope and in one file. – Kevin Kopf Mar 04 '16 at 13:32
  • @apokryfos, I have edited my question. The code does not show any error. The error appears only if I do not use a parameter, i.e., if I wrtie mysqli_insert_id(), which is completely logical as the connection object is expected. My question is about the best way of managing this connection object around all the files and functions of the entire project: as a global variable, as a parameter sent to the functions, as a class, etc., etc. This is not commented in the manual AFAIK. – Cesar Mar 04 '16 at 13:34
  • @Nordenheim, it seems ridiculous because I had the entire project working, and because of the upgrade, now I should add a parameter to all the functions. Instead, a solution like adding a class or something could be better. – Cesar Mar 04 '16 at 13:35
  • @Cesar You could probably use a singleton class (http://www.phptherightway.com/pages/Design-Patterns.html#singleton) – apokryfos Mar 04 '16 at 13:39
  • Yes, I am studying this approach. Thank you very much, @apokryfos. – Cesar Mar 04 '16 at 13:46

1 Answers1

1
class MySQLiConnection { 
      private static $conn = null;
      private function __construct() { }

      public static function getConnection() {
            if (static::$conn === null) { 
                static::$conn = mysqli_connect(/* your params go here */);
            }
            return static::$conn;
      } 
}

Then you can replace all global $conn with $conn = MySQLiConnection::getConnection()

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • I slightly changed the code in order to make it work. I converted a private in public, and dropped the static from the return. Later I called the new(). Is all that right? class MySQLiConnection { private static $conn = null; public function __construct() { } public static function getConnection() { if (static::$conn === null) { $conn = mysqli_connect(/* your parameters go here */); } return $conn; } } $connection = new MySQLiConnection(); $conn = $connection::getConnection(); – Cesar Mar 04 '16 at 14:05
  • The point was you shouldn't need to create an instance of this class because it's just a singleton container. – apokryfos Mar 04 '16 at 14:16
  • I can confirm that it works perfectly without instance, with first private declarations, and the only change to your class definition is in return $conn, which is done directly instead of static::$conn; Thanks!! – Cesar Mar 04 '16 at 14:28
  • @Cesar The mistake was elsewhere, please check the update. It's supposed to be `static::$conn = mysqli_connect` – apokryfos Mar 04 '16 at 14:42