Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2007
    Location
    Manteca, CA
    Posts
    219
    Thanks
    4
    Thanked 0 Times in 0 Posts

    sql code not working in a function ?!?!?

    this is driving me nuts...

    If I execute the following code...

    function MyFunc()
    {
    echo "inside MyFunc - ";
    // $sql = "INSERT INTO test (aline) VALUES('mary had a little lamb')";
    // $result = $database->query($sql);
    }


    // Parse the incoming data
    $q=$_GET["q"];
    parse_str($q);

    $sql = "DELETE from test";
    $result = $database->query($sql);

    MyFunc();

    $sql = "INSERT INTO test (aline) VALUES('mary had a little lamb')";
    $result = $database->query($sql);

    echo "All Done";
    ---------------------------------------------
    Everything works fine... "mary had a little lamb" gets put in the table called "test", and the responsetext in my ajax code gets "MyFunc - All Done".
    --------------------------------------------------
    However, if I uncomment those lines above that insert the 'mary had a little lamb' from within the function, nothing happens (I get a <br> in my responsetext and the db does not get updated. BTW, the reference to $database is just my database object that has the connection already set up and all that. I know this seems like a really dumb question, but it is really making me crazy as I try every iteration possible. I was hoping someone would see something simple going on that I'm simply not seeing.

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    I think the problem may be that your $database object can't be accessed from within the function, in your function add to the start:

    PHP Code:
    global $database

  • #3
    Regular Coder
    Join Date
    May 2007
    Location
    Manteca, CA
    Posts
    219
    Thanks
    4
    Thanked 0 Times in 0 Posts
    THANK YOU! Mann, that was driving me nuts. It does seem a bit weird to me though. If a procedure or method of an object is accessing another object, it has to always include the "global $obj" inside the procedure/method? Alrighty, whatever works I guess.

    Thanks again.

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    It's because the inside of functions is totally separate to the rest of your code, apart from the parameters of the function.

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts

  • #6
    Regular Coder
    Join Date
    May 2007
    Location
    Manteca, CA
    Posts
    219
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I see. what about globals? Is the best way to handle arguments that you want to share between functions to simply make the whole thing a class?


  •  

    Posting Permissions

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