[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Scope within inc.functions.php

Posted on 2007-10-06
3
Medium Priority
?
227 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
0
Comment
Question by:jezella
3 Comments
 
LVL 11

Accepted Solution

by:
f_o_o_k_y earned 2000 total points
ID: 20028165
Hello
First of all in index.php you must include ashley-function.php file before using passwordCheck

Then in ashley-function.php you must first declare $conn and then use it as global in all other functions.

so
[ashley-function.php]
<?php
//      include('secret.php');
$conn = null; //my global connection variable
$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)
            {
global $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)
            {
global $conn;
            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);                        
//something with connection
               }}
?>
[/ashley-function.php]

If you need some more help just ask.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20028717
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.
0
 

Author Comment

by:jezella
ID: 20029757
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);
                  }
            }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question