开发者

Fatal error on a non-object

开发者 https://www.devze.com 2022-12-27 06:06 出处:网络
Hey, so I have created a function to check the DB for unique entries, but when I call the function it doesn\'t seem to work and gives me a fatal error any ideas ? Thanks :)

Hey, so I have created a function to check the DB for unique entries, but when I call the function it doesn't seem to work and gives me a fatal error any ideas ? Thanks :)

 //Check for unique entries
    function checkUnique($table, $field, $compared)
    {
        $query = $mysqli->query('SELECT  '.$mysqli->real_escape_string($field).' FROM '.$mysqli->real_escape_string($table).' WHERE "'.$mysqli->real_escape_string($field).'" = "'.$mysqli->real_escape_string($compared).'"');
        if(!$query){ 
            return TRUE; 
        }   
        else {
            return FALSE;
        }
    }

The page calling it.....

    //Start session
session_start();

//Check if the session is already set, if so re-direct to the game
if(isset($_SESSION['id'], $_SESSION['logged_in'])){
    Header('Location: ../main/index.php');
};

//Require database connection
require_once('../global/includes/db.php');
require_once('../global/functions/functions.php');

//Check if the form has been submitted
if (isset($_POST['signup'])){
    //Validate input
    if (!empty($_POST['username']) && !empty($_POST['password']) && $_POST['password']==$_POST['password_confirm'] && !empty($_POST['email']) && validateEmail($_POST['email']) == TRUE && checkUnique('users', 'email', $_POST['email']) == TRUE && checkUnique('users', 'username', $_POST['username']) == TRUE)
    {
        //Insert user to the database
        $insert_user = $mysqli->query('INSERT INTO (`username, `password`, `email`, `verification_key`) VALUES ("'.$mysqli->real_escape_string($_POST['username']).'", "'.$mysqli-real_escape_string(md5($_POST['password'])).'", "'.$mysqli->real_escape_string($_POST['email']).'", "'.randomString('alnum', 32). '"') or die($mysqli->error());

        //Get user information
        $getUser = $mysqli->query('SELECT id, username, email, verification_key FROM users WHERE username = "'.$mysqli->real_escape_string($_POST['username']).'"' or die($mysqli->error()));

        //Check if the $getUser returns true
        if ($getUser->num_rows == 1)
        {
            //Fetch associated fields to this user
            $row = $getUser->fetch_assoc();

            //Set mail() variables
            $headers = 'From: no-reply@musicbattles.net'."\r\n".
                      'Reply-To: no-reply@musicbattles.net'."\r\n".
                      'X-Mailer:  PHP/'.phpversion();
            $subject = 'Activate your account (Music Battles.net)';
            //Set verification email message
            $message = 'Dear '.$row['username'].', I would like to welcome you to Music Battles. Although in order to enjoy the gmae you must first activate your account. \n\n Click the following link: http://www.musicbattles.net/home/confirm.php?id='.$row['id'].'key='.$row['verification_key'].'\n Thanks for signing up, enjoy the game! \n Music Battles Team';

            //Attempts to send the email
            if (mail($row['email'], $subject, $message, $headers))
            {
                $msg = '<p class="success">Accound has been created, please go activate it from your email.</p>';
            }
            else {
                $error = '<p class="error">The account was created but your email was not sent.</p>';
            }
        }
            else {
                $error = '<p class="error">Your account was not created.</p>';
            }
        }
            else {
                $error = '<p class="error">One or more fields contain non or invalid data.</p>';
            }
        }

Erorr....

Fatal error: Call to a member function query() on a non-object in /home/mbattles/public_开发者_如何学Gohtml/global/functions/functions.php on line 5


$mysqli is not defined inside your function as functions have their own variable scope. Either pass that variable to your function as a parameter:

function checkUnique($mysqli, $table, $field, $compared) {
    // …
}

Or use the global keyword or $GLOBAL variable to access that variable of the global scope inside your function:

function checkUnique($table, $field, $compared) {
    global $mysqli;     // registers the global variable $mysqli locally
    $GLOBALS['mysqli']; // OR access the global variable via $GLOBALS['mysqli']
    // …
}


$mysqli inside your function is not the same $mysqli that is outside the function. You either have to make $mysqli global, or instantiate another db connection.


For me, I had the same issue, but nothing to do with mysql. I had extracted the following method from existing PHP:

<?php function checkMainInner()
{
    ?>
<?php if ($this['modules']->count('innertop')) : ?>
<section id = "innertop" class = "row grid-block"><?php echo $this['modules']->render('innertop', array('layout' => $this['config']->get('innertop'))); ?></section>
<?php endif; ?>

<?php if ($this['modules']->count('breadcrumbs')) : ?>
<section id = "breadcrumbs"><?php echo $this['modules']->render('breadcrumbs'); ?></section>
<?php endif; ?>

<?php if ($this['config']->get('system_output')) : ?>
<section class = "row grid-block"><?php echo $this['template']->render('content'); ?></section>
<?php endif; ?>

<?php if ($this['modules']->count('innerbottom')) : ?>
<section id = "innerbottom" class = "row grid-block"><?php echo $this['modules']->render('innerbottom', array('layout' => $this['config']->get('innerbottom'))); ?></section>
<?php endif;
}

?>

And I made the function call as follows:

<!--if it's just sidebar b-->
        <?php if ($this['modules']->count('sidebar-b') && ($this['modules']->count('sidebar-a') == 0)): ?>
        <div id = "maininner" class = "grid-box ninecol">
            <?php checkMainInner() ?>
        </div>
        <aside id = "sidebar-b" class = "grid-box threecol last">
            <?php echo $this['modules']->render('sidebar-b', array('layout' => 'stack')); ?>
        </aside>
        <?php endif; ?>

Of course, this failed, because $this was out of scope from within the function. So changing the function parameters to take $self as a parameter fixed this:

<?php function checkMainInner($self)
{
    ?>
<?php if ($self['modules']->count('innertop')) : ?>
<section id = "innertop" class = "row grid-block"><?php echo $self['modules']->render('innertop', array('layout' => $self['config']->get('innertop'))); ?></section>
<?php endif; ?>

<?php if ($self['modules']->count('breadcrumbs')) : ?>
<section id = "breadcrumbs"><?php echo $self['modules']->render('breadcrumbs'); ?></section>
<?php endif; ?>

<?php if ($self['config']->get('system_output')) : ?>
<section class = "row grid-block"><?php echo $self['template']->render('content'); ?></section>
<?php endif; ?>

<?php if ($self['modules']->count('innerbottom')) : ?>
<section id = "innerbottom" class = "row grid-block"><?php echo $self['modules']->render('innerbottom', array('layout' => $self['config']->get('innerbottom'))); ?></section>
<?php endif;
}

?>

I then the new method call:

<!--if it's just sidebar b-->
        <?php if ($this['modules']->count('sidebar-b') && ($this['modules']->count('sidebar-a') == 0)): ?>
        <div id = "maininner" class = "grid-box ninecol">
            <?php checkMainInner($this) ?>
        </div>
        <aside id = "sidebar-b" class = "grid-box threecol last">
            <?php echo $this['modules']->render('sidebar-b', array('layout' => 'stack')); ?>
        </aside>
        <?php endif; ?>

Doing this fixed my problems. Hopefully, this helps someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消