1

I recently moved an app from using the older mysql funcions to using mysqli functions. The app connects to the DB on each request, and with the old functions you didn't need to pass in a DB connection, but with the new functions you do. I have placed the connection in the GLOBALS array to be used by my mysql library.

$GLOBALS['connection'] = mysqli_connect($host, $user, $pass, $db);

Question 1: Is there any reason not to do this?

Question 2: Everything seems to be working fine when I access the app, but Googlebot seems to be having trouble:

mysqli_real_escape_string() expects parameter 1 to be mysqli, null given

When the connection is being passed in from the $GLOBALS array. Anny idea why Googlebot might cause the DB connection to not exist?

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Sherms
  • 1,567
  • 1
  • 15
  • 31

1 Answers1

0

Depending on the complexity of your application, I would consider switching to a dedicated class (Singleton) that will handle everything related with your database.

Some discussion here: Global or Singleton for database connection?

In addition, depending on the same question again, you may want to look at PDO, which is the new way of accessing databases in PHP.

Community
  • 1
  • 1
Pierre-Olivier
  • 3,104
  • 19
  • 37