Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 715113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:09:02+00:00 2026-05-14T05:09:02+00:00

Hey, so I have created a function to check the DB for unique entries,

  • 0

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_html/global/functions/functions.php on line 5
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:09:03+00:00Added an answer on May 14, 2026 at 5:09 am

    $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']
        // …
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.