Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 非对象上的致命错误_Php_Function_Mysqli - Fatal编程技术网

Php 非对象上的致命错误

Php 非对象上的致命错误,php,function,mysqli,Php,Function,Mysqli,嘿,我创建了一个函数来检查数据库中是否有唯一的条目,但是当我调用这个函数时,它似乎不起作用,并且给了我一个致命的错误,有什么想法吗?谢谢:) 页面称之为 //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('Loca

嘿,我创建了一个函数来检查数据库中是否有唯一的条目,但是当我调用这个函数时,它似乎不起作用,并且给了我一个致命的错误,有什么想法吗?谢谢:)

页面称之为

    //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>';
            }
        }

$mysqli
未在函数中定义,因为函数有自己的函数。将该变量作为参数传递给函数:

function checkUnique($mysqli, $table, $field, $compared) {
    // …
}
或者使用或
$GLOBAL
变量访问函数中全局范围的变量:

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

$mysqli
未在函数中定义,因为函数有自己的函数。将该变量作为参数传递给函数:

function checkUnique($mysqli, $table, $field, $compared) {
    // …
}
或者使用或
$GLOBAL
变量访问函数中全局范围的变量:

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

函数内的$mysqli与函数外的$mysqli不同。您必须使$mysqli成为全局的,或者实例化另一个数据库连接。

$mysqli在函数内部与函数外部不同。你要么将$mysqli设置为全局,要么实例化另一个数据库连接。

对我来说,我也有同样的问题,但与mysql无关。我从现有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;
}

?>
然后,我将调用新方法:

<!--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; ?>


这样做解决了我的问题。希望这能帮助一些人。

对我来说,我也有同样的问题,但与mysql无关。我从现有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;
}

?>
然后,我将调用新方法:

<!--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; ?>


这样做解决了我的问题。希望这对某人有所帮助。

$mysqli未在您的函数中设置。函数中的变量位于不同的范围内,因此与函数外的变量不同。另外,我在主脚本中也没有看到$mysqli的任何定义。我假设它来自db.php,是这样吗?$mysqli没有在函数中设置。函数中的变量位于不同的范围内,因此与函数外的变量不同。另外,我在主脚本中也没有看到$mysqli的任何定义。我假设它来自db.php,是这样吗?为什么你不直接以
$mysqli
的形式访问变量,而不是
$GLOBALS['mysqli']
(在函数的第一行声明了
global
之后)?@brianreavis:这只是一个显示两个变量的示例。哦,不用担心!我只是真的很好奇。。。仅此而已。为什么不以
$mysqli
的形式访问变量,而不是
$GLOBALS['mysqli']
(在函数的第一行声明了
global
之后)?@brianreavis:这只是一个显示这两个变量的示例。哦,不用担心!我只是真的很好奇。。。这就是全部。