PHP和MySQLi:'返回$ mysqli_object'不会返回任何内容[关闭]

everyone!

i have a strange bug with MySQLi, i am using a connect() function that (supposedly) returns a mysqli object, but it doesn't seem to return anything.

Here is my function:

    function Connect() {
        global $dbg;
        if( $dbg === false ) {
            echo("test 1");
            $dbg = new mysqli ("localhost", "root", "", "erp");
            if ($dbg->connect_errno) {
                echo "( " . $dbg->connect_errno . ") " . $dbg->connect_error;
            }
            echo("test 2");
        }
    return $dbg;
    echo("test 3");
    }

This print to screen "test 1" and "test 2", but not "test 3".
It would seem that php is stuck there, as there is an include of a file containing all the graphic settings following this, and the graphics doesn't print to screen.

Funny thing :

    function Connect() {
        global $dbg;
        if( $dbg === false ) {
            $dbg = new mysqli ("localhost", "root", "", "erp");
            if ($dbg->connect_errno) {
                echo "( " . $dbg->connect_errno . ") " . $dbg->connect_error;
            }
        }
    $st = $dbg->prepare('INSERT INTO testtable(ID) VALUES (1)');
    $st->execute();
    return $dbg;
    }

This WORKS. The insert is done So i guess this return $dbg; is what makes all go to hell, but i don't know where the problem comes from in my Web Server.

It all worked without a glitch locally on Wamp, but doesn't work anymore when deployed to the webserver. The only thing that comes to mind is that php 4.something is installed on the webserver and my wamp is using php5. Can it come from there?

Thanks for reading guys!

if you want to see the echo put it before the return

echo("test 3");
return $dbg;

otherwise the code won't be executed.

As for getting mysqli to work on your server you will need to make sure your PHP is version 5 or above.

Sorry, my bad, the test was done with the echo BEFORE the return, added the code here myself on forum

And, Mike W, as much as it requires php5, i still succeeded doing an insert. It's amazing what you can do with unitary tests, don't you think? As a matter of fact, MySQLi is advised since MySQL 4.1.3, you just have to check/mod your php.ini file

And sorry again to have bothered you guys, found my problem :

  • As stupid as it gets, i was using global variables with register_globals=0, so it couldn't work.
  • Then i had another error since i didn't have the mysqlnd driver now pretty much required

Thanks again
Cordially,
Zawarudio