嘿,所以我创建了一个函数来检查数据库是否有唯一的条目,但是当我调用该函数它似乎不起作用并给我一个致命的错误任何想法?谢谢:)
//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: 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_html/global/functions/functions.php on line 5
答案 0 :(得分: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']
// …
}
答案 1 :(得分:1)
答案 2 :(得分: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; ?>
这样做解决了我的问题。希望,这有助于某人。