Link to home
Start Free TrialLog in
Avatar of jezella
jezellaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Scope within inc.functions.php

I feel a real fool here as the mistake I'm making really must be very simple but I can see it though I do realize that it is scope related. I thought I understood global scope and local scope but obviously no.

I'm learning php and this a small project I've set myself to aid learning.

In index.php I do various checks on the site visitor and I include('ashley-functions.php').  Most seems to work fine and I am able to connect to the server ok.  I then call function passwordCheck($user, $password) where should both a usename and password be present a database should be selected and connected to.  The problem is that $conn is not a valid Mysql_link.

I need to know why the link has broken. I have set global $conn but this does not appear to be global within the functions.php

Code follows.

index.php

      @$session    =session_start();

      @$username   =$_POST['username'];
      @$password   =$_POST['password'];
      @$requestType=$_POST['formsubmit'];
      @$submitForm =$_SESSION['userType'] = $requestType;
      if (!isset($submitForm))
            {
            include('login-register.php');
            exit;
            }

      if ($submitForm == 'registerRequest')
            {
            include('registration-form.php');
            exit;
            }
             
      if ($submitForm == 'joinRequest')
            {
            passwordCheck($username, $password);
            }
      ?>


ashley-functions.php

<?php
//      include('secret.php');

$db='members';


//      Connect to server

function serverConnect($mysqlHost, $mysqlUser, $mysqlPassword)
      {
      include('secret.php');
      global $conn;
      @$conn=mysql_connect($mysqlHost, $mysqlUser, $mysqlPassword);
      //return $conn;
      
      if ($conn)    
            echo "Connected to Server<br />";
      
      else
            {
            echo "Connection to the server failed.<br />\n
                  </body>\n
            </html>";
            exit;
            }
      }

//      Select Database
      function selectDb($db, $conn)
            {
            $connDb=mysql_select_db($db, $conn);
             if (!$connDb)
                  {
                  echo 'Sorry, We were Unable to Connect to the Database.';
                  exit;
                  }
                  else echo "Connected to Database";
            }
            
      function passwordCheck ($username, $password)
            {
            if (empty($username) OR empty($password))
                  {
                  include('registration-form.php');
                  echo '<br /><h3 align = "center">Please suppy all info</h3>';
                  exit;
                  }
            else
                  {
            //      serverConnect($mysqlHost, $mysqlUser, $mysqlPassword);
                  selectDb($db, $conn);                        
               }}
?>

Many thanks for the attention.

Jezella

PS

How do I set Experts-Exchange so that I am notified that a comments has been added.

Ta
ASKER CERTIFIED SOLUTION
Avatar of f_o_o_k_y
f_o_o_k_y
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The important thing (and why your script is not working) is simply because you do not have "global $conn;" in your function passwordCheck().

Simply put, insert this line into your function and all should work without scoping problems!
ie:

function passwordCheck($username, $password)
{
$global $conn;
// The rest of your code here....
}

Let me know how you get on.
Avatar of jezella

ASKER

fooky and nizsmo , thanks for the help. Basically, these comments answered the question but more important made me think harder about why the above still did not permit the function to work as intended. Sorry, the copy and paste missed the include('ashley-functions.php in index.php').

For the benefit of other readers I was making two mistakes. The global $conn being missing as mentioned above plus I had failed to make the $db global.  So the following code now allows the function to work as intended.

The following may not be perfect in layout but I'm getting better.

Many thanks.

function passwordCheck($username, $password)
            {
                  global $conn;
                  global $db;
            if (empty($username) OR empty($password))
                  {
                  include ('registration-form.php');
                  echo '<br /><h3 align = "center">Please suppy all info</h3>';
                  exit;
                  }
            else
                  {
                  serverConnect($mysqlHost, $mysqlUser, $mysqlPassword);
                  selectDb($db, $conn);
                  }
            }