#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7

    MySQLi function issue with Prepared Statement


    I have this same setup, pretty much, in my login script, and all works fine. I am thinking it may be the way I'm running it in a function?

    Called like: updateIOStatus($_POST['inout']);
    $_POST['inout'] sucessfully holds integer 1 or 0.
    Page just stops loading at this point, but prints items prior to. No error shown.

    PHP Code:
    function updateIOStatus($val) {
      
    $statement $link->prepare("UPDATE `userpass` SET `OutOfOffice` = ? WHERE `Username` = ?");
      
    $statement->bind_param('is'$val$_SESSION['user']);
      
    $statement->execute();
      
    $statement->close();


    EDIT: I tossed ECHO's around echo line to see how far script makes it. It appears to be failing on my prepare line.
    Last edited by Triple_Nothing; February 3rd, 2013 at 12:47 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Hmmm.... I found out the db.php file that I have included at the very top on the template that is holding all these files as includes does not carry over? If I add the db.php directly into the function, all works. Any ideas?

    index.php
    --include('db.php');
    --include('modules/' . $_GET['loc'] . '/index.php');


    modules/main/index.php (As included above via $_GET['loc'])
    --include('functions.php'); <-- Is not connecting to included db.php above. :-/
    Last edited by Triple_Nothing; February 3rd, 2013 at 12:56 PM.
  4. #3
  5. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Functions do not have access to variables defined outside of that function, as such your $link variable does not exist. You need to pass it into the function as a parameter.

    The PHP defined super-global variables ($_SESSION, $_POST, etc) are special and are not affected by this. They exist in all scopes automatically.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Thank you. That pretty much covered all I needed. ^_^
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    for this particular case, I'd rather access the variable via $GLOBALS than pass it to every single function which uses the database.

    PHP Code:
    function updateIOStatus($val) {
        global 
    $link;
        
    $statement $link->prepare("UPDATE `userpass` SET `OutOfOffice` = ? WHERE `Username` = ?");
        
    # ...


IMN logo majestic logo threadwatch logo seochat tools logo