Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.