Jump to content

no global vars in function using cake?


micah1701

Recommended Posts

I'm new to frameworks and the whole model view control thing so please go easy on me...

 

I have a page that is included() inside a view.  In this page, I have a function() that check needs to do a mysql query that needs to know the database name which I keep in a variable.  Apparently though, I can't just declare it as a variable any more?

 

<?php
$db = "database";
function sql(){
global $db;
return "database name is: ".$db." ta-da!";
} 

echo sql();// returns: database name is: ta-da!

 

why is this?

Link to comment
https://forums.phpfreaks.com/topic/184801-no-global-vars-in-function-using-cake/
Share on other sites

<?php
global $db;
$db = "database";
function sql(){
global $db;
return "database name is: ".$db." ta-da!";
} 

echo sql();// returns: database name is: ta-da!

 

You need to declare it as a DB outside the function as well. You may want to look into defineing the variable as a CONSTANT instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.