|  | Posted by Ney Andrι de Mello Zunino on 06/20/13 11:57 
ZeldorBlat wrote:
 > You need to put global $blog; inside your functions.  This tells PHP to
 > "bind" the variable $blog inside the function to the global one.
 > Adding that should fix it...although personally I prefer not to use
 > globals.
 
 Thanks for your quick reply. You are right: globals are evil, at least
 most of the time. I reworked the code, which now looks like:
 
 // =====Begin sample code=====
 
 function dbConnect()
 {
 $conn = mysql_connect(constant("dbHost"),
 constant("dbUser"),
 constant("dbPassword"))
 or die("DB connection error: " . mysql_error());
 mysql_select_db('blog')
 or die("Error selecting blog database: " . mysql_error());
 return $conn;
 }
 
 function dbDisconnect($conn)
 {
 mysql_close($conn);
 }
 
 $blog["dbconn"] = dbConnect();
 ..
 ..
 ..
 dbDisconnect($blog["dbconn"]);
 
 // =====End sample code=====
 
 I guess it has improved a bit, hasn't it?
 
 Cheers!
 
 --
 Ney AndrΓ© de Mello Zunino
  Navigation: [Reply to this message] |