How to have global variable in PHP script?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Sun Aug 24, 2003 8:32 pm

What is the error you are getting?

You could be passing the variables to the page using GET or POST. Like how you have you're form, when the user clicks refresh, when they have 5 in the input box. You could be using a GET method of form sending, sending it to the page which picks it up.

so you will have a URL like page.php?limit=5

Then on the page with the SQL query you can have some code like this.

Code: Select all

if (isset($_GET['limit'])) {
   $limit = $_GET['limit'];
   settype($limit, 'int'); //Leaves only integer values
}
else {
   $limit = 300; //Just a large value
}

$sql = "SELECT * FROM table ORDER BY field1 LIMIT 0, ".$limit;

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1279
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Sun Aug 24, 2003 9:09 pm

I don't know why you require a global variable, but if the variable is for a particular user, you can use sessions and then set a particualar varible which you want it globally!
:idea:

sigix
php-forum Active User
php-forum Active User
Posts: 364
Joined: Mon Jul 14, 2003 9:39 pm
Location: /Earth/Xion

Mon Aug 25, 2003 1:15 am

.....or sessions not solution :arrow:


User avatar
swirlee
Moderator
Moderator
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Mon Aug 25, 2003 12:32 pm

raulgh5 wrote:Global g_sql //this is the global var


First of all, unless g_sql is a constan (as created by define()), then it needs a $ before it just like every other variable. Second, if you really want a global variable (as defined in the documentation, as I have a sneaking suspicion that you have the wrong idea of what a global variable is), the you have to declare the variable global from within the function that's referencing it:

Code: Select all

<?
$a = 1; // the variable you want to be global -- right now it isn't

function add_one() {
   global $a; // now the variable can be accessed from within this function
   $a++;
}

add_one(); // add one to $a
echo $a; // this will output '2';
?>


The other way to do this is using pass-by-reference:

Code: Select all

<?
$a = 1; // look familiar?

function add_one(&$var) { // see the &? that means pass it by reference
   $var++;
}

add_one($a); // add one to $a;
echo $a; // this will output '2'
?>


Now do me a favor and read both of those links very thoroughly. Don't just copy my code and expect it to work. Of course, the example above is a bad one, because it'd be better to just to do this:

Code: Select all

<?
   $a = 1;

   function add_one($var) {
      return $var++;
   }

   $a = add_one($a); // add one to $a, assign the result to $a
   echo $a; // this will output '2'
?>
Image

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Mon Aug 25, 2003 1:27 pm

Why is the $SQL_stmt becoming null after the function? Is there something in the function that is doing this? If you only need the SQL statement variable available on one page then you shouldn't need to do anything special to reach it anywhere AFTER it has been assigned.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests