We help IT Professionals succeed at work.

Scope within inc.functions.php

jezella
jezella asked
on
245 Views
Last Modified: 2013-12-13
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
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Joe WuDeveloper | Solutions Architect | CTO
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
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);
                  }
            }
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.