Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Accessing a variable in a function from the main script

  1. #1
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default Accessing a variable in a function from the main script

    G'day all!

    Say I've got this function that connects to the mysql database -
    PHP Code:
    function sqli_connect($username$password) {
            
    $connection mysqli_connect("localhost"$username$password);
            
    //Test connection to database
            
    if(mysqli_connect_errno()) {
                echo 
    "Installation error: Could not connect to database.";
                exit;
            }
        } 
    Then I call it from here -
    Code:
    <?php
    sqli_connect('****', '****');
    //Create the databases
    if(mysqli_query($connection,"CREATE DATABASE my_db")) { //Executes mysqli query and tests for a return of true at the same time
    	echo "Database my_db created successfully";
    } else {
    	echo "Error creating database: " . mysqli_error($con);
    }
    ?>
    How can I give it the connection variable from the function?
    Thanks all
    keebs

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    Don't use a function like that. I usually use an include for setting up a database connection.

    Or make it a global variable within the function:
    PHP Code:
    function sqli_connect($username$password) {
            
    $GLOBALS['connection'] = mysqli_connect("localhost"$username$password);
            
    //Test connection to database
            
    if(mysqli_connect_errno()) {
                echo 
    "Installation error: Could not connect to database.";
                exit;
            }
        } 
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    I'm using a function because the user inputs the mysql login details (don't ask ) so it's a lot easier to just put that stuff into some parameters then using an include (or a require).

    Thanks for the globals bit, I'll try that out

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    Technically there's no reason you couldn't still use an include-- it can be effectively like a function, if you pre-set certain variables before the include. But the globals method will work.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Something really weird is going on o.O

    PHP Code:
    <?php
        
    function sqli_connect($username$password) {
            
    $sqli_connection mysqli_connect('localhost'$username$password);
            if(!
    $sqli_connection) {
                return 
    'false'//Could not connect, return false.
            
    } else {
                return 
    'true'//Connection fine, return true.
            
    }
        }
    ?>
    (for the moment I pulled out the globals bit)

    If the connection is valid, it returns true.
    If I put in an empty password, no matter the username, I get a true return though.. no clue why. But it's not a valid connection because I ran an sql command and nothing happened.
    If I put in an invalid username and a password that isn't empty, or I put in the right username and a password that is wrong (and not empty) I get a php error, it doesn't return false..

    So two questions -
    1. Any clue why this is happening?
    2. How can I stop the php error for just this event?


    Edit: EDIT -

    Actually, just figured out how to stop the php error.. just used an @ on the mysql connect.

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by keyboard1333 View Post
    Something really weird is going on o.O

    PHP Code:
    <?php
        
    function sqli_connect($username$password) {
            
    $sqli_connection mysqli_connect('localhost'$username$password);
            if(!
    $sqli_connection) {
                return 
    'false'//Could not connect, return false.
            
    } else {
                return 
    'true'//Connection fine, return true.
            
    }
        }
    ?>
    (for the moment I pulled out the globals bit)

    If the connection is valid, it returns true.
    If I put in an empty password, no matter the username, I get a true return though.. no clue why. But it's not a valid connection because I ran an sql command and nothing happened.
    If I put in an invalid username and a password that isn't empty, or I put in the right username and a password that is wrong (and not empty) I get a php error, it doesn't return false..

    So two questions -
    1. Any clue why this is happening?
    2. How can I stop the php error for just this event?
    1. Is there a reason you're returning true/false as strings?

    2. What is the php error you're getting?


    Quote Originally Posted by keyboard1333 View Post
    Edit: EDIT -

    Actually, just figured out how to stop the php error.. just used an @ on the mysql connect.
    ugh, please don't do that.

    There are some valid uses for the error suppression operator ( @ ), but this isn't one of them.
    This is like putting a fancy rug over the spot where the dog barfed so you don't have to clean it up.

    NOW, to your original question:
    Quote Originally Posted by keyboard1333 View Post
    Say I've got this function that connects to the mysql database -
    PHP Code:
    function sqli_connect($username$password) {
            
    $connection mysqli_connect("localhost"$username$password);
            
    //Test connection to database
            
    if(mysqli_connect_errno()) {
                echo 
    "Installation error: Could not connect to database.";
                exit;
            }
        } 
    Then I call it from here -
    Code:
    <?php
    sqli_connect('****', '****');
    //Create the databases
    if(mysqli_query($connection,"CREATE DATABASE my_db")) { //Executes mysqli query and tests for a return of true at the same time
    	echo "Database my_db created successfully";
    } else {
    	echo "Error creating database: " . mysqli_error($con);
    }
    ?>
    How can I give it the connection variable from the function?
    Thanks all
    keebs
    Two methods pop to mind. #1 (preferred): return the connection from the function.
    PHP Code:
    <?php

    function sqli_connect($username$password) {
        
    $connection mysqli_connect("localhost"$username$password);
        
    //Test connection to database
        
    if(mysqli_connect_errno()) {
            return 
    false;  // connection failed
        
    }
        return 
    $connection;  // connection successful
    }

    //  $link will be a mysqli connection on success; or FALSE on failure.
    $link sqli_connect'username','password' );
    #2: If the function *must* return true/false for some reason, you can pass a var for the connection by reference.
    PHP Code:
    <?php

    function sqli_connect$username,$password,$link=null ) {
        
    $link mysqli_connect("localhost"$username$password);
        
    //Test connection to database
        
    if(mysqli_connect_errno()) {
            return 
    false;  // connection failed
        
    }
        return 
    true;  // connection successful
    }

    //  $link will be a mysqli connection on success; or FALSE on failure.
    //  $success will be TRUE on success; or FALSE on failure.
    $success sqli_connect'username','password',$link );
    I don't recommend using $_GLOBALS, for various reasons.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  7. #7
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Quote Originally Posted by traq View Post
    1. Is there a reason you're returning true/false as strings?
    I was just using that while debugging so I could see which it was returning easier (personal preference)

    Quote Originally Posted by traq View Post
    2. What is the php error you're getting?
    The wrong username/password error. -
    Warning: mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (28000/1045): Access denied for user 'test'@'localhost' (using password: YES) in C:\wamp\www\****\connect_mysql.php on line 3
    Quote Originally Posted by traq View Post
    ugh, please don't do that.

    There are some valid uses for the error suppression operator ( @ ), but this isn't one of them.
    This is like putting a fancy rug over the spot where the dog barfed so you don't have to clean it up.
    Hahahhaa, I'd just like to stop the PHP error and display my own cleaner one.
    Quote Originally Posted by traq View Post
    NOW, to your original question:

    Two methods pop to mind. #1 (preferred): return the connection from the function.
    PHP Code:
    <?php

    function sqli_connect($username$password) {
        
    $connection mysqli_connect("localhost"$username$password);
        
    //Test connection to database
        
    if(mysqli_connect_errno()) {
            return 
    false;  // connection failed
        
    }
        return 
    $connection;  // connection successful
    }

    //  $link will be a mysqli connection on success; or FALSE on failure.
    $link sqli_connect'username','password' );
    Doesn't that mean that everytime you want to access the connection, you have to make a new connection? Or would you assign it to a variable?

    Quote Originally Posted by traq View Post
    #2: If the function *must* return true/false for some reason, you can pass a var for the connection by reference.
    PHP Code:
    <?php

    function sqli_connect$username,$password,&$link=null ) {
        
    $link mysqli_connect("localhost"$username$password);
        
    //Test connection to database
        
    if(mysqli_connect_errno()) {
            return 
    false;  // connection failed
        
    }
        return 
    true;  // connection successful
    }

    //  $link will be a mysqli connection on success; or FALSE on failure.
    //  $success will be TRUE on success; or FALSE on failure.
    $success sqli_connect'username','password',$link );
    I don't recommend using $GLOBALS, for various reasons.
    Really have no clue what you mean :\
    Last edited by traq; 05-16-2013 at 06:46 PM. Reason: corrections Daniel pointed out

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    ugh, please don't do that.

    There are some valid uses for the error suppression operator ( @ ), but this isn't one of them.
    This is like putting a fancy rug over the spot where the dog barfed so you don't have to clean it up.
    Hahahhaa, I'd just like to stop the PHP error and display my own cleaner one.
    I think I agree in this case-- usually error suppression is bad, but your code is not bad-- your account (user/pass) is incorrect, and you won't know what the user has entered. Now, do you actually want to make that possible? It sounds a little dangerous to me. But that's a separate issue. If you expect to potentially get back into from the client, then it's reasonable to suppress errors that generates as long as you handle everything cleanly after that (such as not continuing with the rest of the script!).


    ---
    Traq:

    #2: If the function *must* return true/false for some reason, you can pass a var for the connection by reference.

    [...code...]
    I'm not seeing anything special in your code. Is this due to having an undefined variable in the function call? I haven't used this method myself, and I think I'm missing something as well.

    Additionally, I believe that methods like that (though I don't entirely understand them) vary significantly by PHP version.

    I don't recommend using $_GLOBALS, for various reasons.
    I have three responses here:
    1. It's $GLOBALS, with no underscore. I still probably make that mistake myself most times I type it, but it's important to remind yourself of that odd difference in spelling (compared to _GET, _POST, etc.).

    2. I like using $GLOBALS, although it can cause problems if you might not be in the global scope (a function within a function). Assuming you know you're at the global scope for some reason, I don't see a problem using it-- but it shouldn't be relied upon arbitrarily if you aren't sure. (I like using some global variables such as system settings. If needed, you can create an array and work within that global array, if you're worried about overlapping variable names.)
    Note that I do not recommend ever using the global keyword. I strongly prefer $GLOBALS. The reasons are 1) you're overtly using a global variable each time, and 2) it won't accidentally do anything weird to the variable within the function-- you're always referring to $GLOBALS['var'] and intentionally changing it. The potential danger of globals is that you might mess up the global scope within the function's scope. But that danger is much less when overtly using $GLOBALS.

    3. This particular case seems like a great example-- you only want one connection per page (right, kb??), so you'd want that to be global (accessible from anywhere), and you wouldn't have any issues of duplicating the variable, etc.
    If it happens to be that this might be some kind of function/include with a MySQL connect for a secondary activity-- connecting to an external host for example, so that you can transfer to the local MySQL DB as a backup-- then you would not want global scope. That's where a class would become handy, I think. But I don't know that it's relevant here. Usually, your main MySQL connection should be global, unless you have multiple connections.




    ---

    However, the best answer (better than mine above) is to return the connection as the output. You can also alternatively (with "if") return FALSE as needed, in the event that $link failed for whatever reason. You can also test for TRUE/FALSE based on $link, so you don't need the function itself to return TRUE/FALSE.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,623
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    Traq:

    I'm not seeing anything special in your code. Is this due to having an undefined variable in the function call? I haven't used this method myself, and I think I'm missing something as well.
    I made an oversight. function sqli_connect( $username,$password,$link=null ){ ... should be function sqli_connect( $username,$password,&$link=null ){ .... I'm very sorry for the confusion. That & passes the variable $link by reference: instead of copying the value into the function's scope (as normally happens), it literally points at the same value, so anything you do to $link inside the function happens to $link outside the function as well.

    Quote Originally Posted by djr33 View Post
    1. It's $GLOBALS, with no underscore. I still probably make that mistake myself most times I type it, but it's important to remind yourself of that odd difference in spelling (compared to _GET, _POST, etc.).
    Quite right. I corrected that too.

    Quote Originally Posted by djr33 View Post
    2. I like using $GLOBALS, although it can cause problems if you might not be in the global scope (a function within a function). Assuming you know you're at the global scope for some reason, I don't see a problem using it-- but it shouldn't be relied upon arbitrarily if you aren't sure. (I like using some global variables such as system settings. If needed, you can create an array and work within that global array, if you're worried about overlapping variable names.)
    Note that I do not recommend ever using the global keyword. I strongly prefer $GLOBALS. The reasons are 1) you're overtly using a global variable each time, and 2) it won't accidentally do anything weird to the variable within the function-- you're always referring to $GLOBALS['var'] and intentionally changing it. The potential danger of globals is that you might mess up the global scope within the function's scope. But that danger is much less when overtly using $GLOBALS.
    Agreed. The potential to confuse scope - function-within-function (or class) - is the main reason I avoid it.


    Quote Originally Posted by keyboard1333 View Post
    I was just using that while debugging so I could see which it was returning easier (personal preference)
    Alright - and sorry, I didn't mean to sound upset in my last post. Had a rough day.

    What I was getting at is, since you're returning "true" or "false" (as strings), when you test if( ! $sqli_connection ), it will always evaluate as TRUE. (The string, "false", is boolean TRUE.)


    Quote Originally Posted by keyboard1333 View Post
    Hahahhaa, I'd just like to stop the PHP error and display my own cleaner one.
    That's a good use. I had thought you just wanted to ignore the error.

    Quote Originally Posted by keyboard1333 View Post
    Doesn't that mean that everytime you want to access the connection, you have to make a new connection? Or would you assign it to a variable?
    Assigning it to a variable is what I had in mind. Note the correction I made (&) after Daniel's comment.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  10. The Following User Says Thank You to traq For This Useful Post:

    djr33 (05-16-2013)

  11. #10
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Quote Originally Posted by traq View Post
    I made an oversight. function sqli_connect( $username,$password,$link=null ){ ... should be function sqli_connect( $username,$password,&$link=null ){ .... I'm very sorry for the confusion. That & passes the variable $link by reference: instead of copying the value into the function's scope (as normally happens), it literally points at the same value, so anything you do to $link inside the function happens to $link outside the function as well.
    That's interesting. It's the & that makes it work like that yes?


    Quote Originally Posted by traq View Post
    Alright - and sorry, I didn't mean to sound upset in my last post. Had a rough day.

    What I was getting at is, since you're returning "true" or "false" (as strings), when you test if( ! $sqli_connection ), it will always evaluate as TRUE. (The string, "false", is boolean TRUE.)
    At the time, I'd commented out my if in that format, and was just echoing the value of the function. Yeah.

    Quote Originally Posted by traq View Post
    That's a good use. I had thought you just wanted to ignore the error.
    Really? I thought you meant what I was doing was a bad thing to be doing.

    Quote Originally Posted by traq View Post
    Assigning it to a variable is what I had in mind. Note the correction I made (&) after Daniel's comment.
    [/quote]
    Ahhh ok. So I'd just test if it returned false, else assign to variable.. yeah

    I think I agree in this case-- usually error suppression is bad, but your code is not bad-- your account (user/pass) is incorrect, and you won't know what the user has entered. Now, do you actually want to make that possible? It sounds a little dangerous to me. But that's a separate issue. If you expect to potentially get back into from the client, then it's reasonable to suppress errors that generates as long as you handle everything cleanly after that (such as not continuing with the rest of the script!).
    I'm not quite sure what you're getting at there.

Similar Threads

  1. Calling a function in Iframe from main page help please!
    By shellymabelly83 in forum JavaScript
    Replies: 4
    Last Post: 12-14-2011, 01:16 AM
  2. AJAX TABS accessing ajax script with field variable
    By euro121253 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 09-29-2009, 05:55 PM
  3. Replies: 1
    Last Post: 06-19-2009, 01:53 AM
  4. Accessing A javascript variable in HTML
    By rehanazher in forum JavaScript
    Replies: 1
    Last Post: 03-16-2009, 03:18 AM
  5. Replies: 2
    Last Post: 09-06-2006, 05:11 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •