0

I'm feeling my way around php for the first time in years. I'm trying to perform a simple select statement. I've confirmed the statement works directly against mysql. My php does not complete. I'd like to know why my real_query isn't working, I'd also like to know how to coax an error message out of this scenario. Here's the code:

function getRoot($nid)
{
    echo $nid; //displays 0
    try 
    {
        echo "Hello?"; //This displays

        //Why doesn't this work?!
        if($mysqli->real_query("SELECT * FROM gem, bundles WHERE gem.nid = bundles.baseNid AND bundles.nid = " . $nid))
        {
            echo "dafuq?"; //does not display
        }
        else
        {
            echo "foo"; //doesn't display
            echo $mysqli->error; //doesn't display
        }
    }
    catch (Exception $e)
    {
        echo "Tralalalala"; //doesn't display
    }
    echo "teeheehee"; //doesn't display
}

Thanks for your help!

Billdr
  • 1,468
  • 1
  • 15
  • 29
  • 1
    Do you have `error_reporting` enabled and `display_errors` on in your php.ini? (Assuming it's a development machine, set error_reporting to E_ALL|E_STRICT and display_errors to On). – Maerlyn May 29 '13 at 10:44
  • Where is `$mysqli` defined? – Hanky Panky May 29 '13 at 10:47
  • apparently,either its not the right way or you are having an eye sight problem.. o btw, i learnt to think global when i started php.. :D – argentum47 May 29 '13 at 10:49
  • @Maerlyn That was the problem with coaxing the error out, thanks. – Billdr May 29 '13 at 10:56
  • @cweiske Half of it is a duplicate, however the source of my issue was a problem with variable scoping. That's probably a duplicate of something too. – Billdr May 29 '13 at 10:57

2 Answers2

6

There is no $mysqli variable declared in the function, so your code produce a FATAL ERROR - you are calling a method of a non-object. if $mysqli is a global variable, you have to add global $mysqli; in the beginning of the function

Maxim Krizhanovsky
  • 24,757
  • 5
  • 49
  • 85
4

$mysqli is not defined in the function, and PHP throws a fatal error, which is not catcheable using standard exceptions.

You need to enable error reporting (display_errors set to On in php.ini, or through ini_set, eg: ini_set('display_errors', '1'); );

Palantir
  • 22,691
  • 9
  • 74
  • 84