2010-04-13 129 views
0

嘿,所以我创建了一个函数来检查数据库的唯一条目,但是当我调用函数时,它似乎不工作,并给我一个致命的错误任何想法?谢谢:)致命错误非对象

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

的页面调用它.....

//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: [email protected]'."\r\n". 
         'Reply-To: [email protected]'."\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_html/global/functions/functions.php on line 5 
+0

$ mysqli没有在你的函数中设置。函数中的变量在不同的范围内,因此与函数外部的变量不同。另外,我的主脚本中也没有看到$ mysqli的任何定义。我认为它来自db.php,这是这种情况? – erisco 2010-04-13 16:26:54

回答

3

$mysqli没有自己的函数中定义函数有自己的variable scope。要么变量传递给你的函数的参数:

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

或者使用global keyword$GLOBAL变量来访问你的函数内部的全球范围的变量:

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

为什么不直接使用'$ mysqli'而不是'$ GLOBALS ['mysqli']'(在函数的第一行声明'global'后)? – brianreavis 2010-04-13 16:33:04

+0

@brianreavis:这只是一个显示两种变体的例子。 – Gumbo 2010-04-13 16:44:21

+0

哦,不用担心!我真的很好奇......就这些。 – brianreavis 2010-04-13 20:29:34

1

$ mysqli的你的函数里面不同于功能之外的$ mysqli。您必须使$ mysqli全局,或实例化另一个数据库连接。

0

对我来说,我有同样的问题,但与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() ?> 
     </div> 
     <aside id = "sidebar-b" class = "grid-box threecol last"> 
      <?php echo $this['modules']->render('sidebar-b', array('layout' => 'stack')); ?> 
     </aside> 
     <?php endif; ?> 

当然,这种失败,因为$this是超出范围从函数中。因此,改变功能参数,采取$self作为固定这样的参数:

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

?> 

我那么新的方法调用:

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

这样做固定我的问题。希望这可以帮助某人。

相关问题