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 3796780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:17:44+00:00 2026-05-19T13:17:44+00:00

I am trying to create my own class to handle mysql interactions on a

  • 0

I am trying to create my own class to handle mysql interactions on a test site I’m making. I can’t get the mysqli_connect() to work within the class declaration. Every time I try to use the class in a script I get this syntax error:

Parse error: syntax error, unexpected ‘(‘, expecting ‘,’ or ‘;’ in C:\xampp\htdocs\autoshop\classes\class_db.inc.php on line 7

Here is the code I have so far:

<?php 
class Db{
    private $host = 'localhost';
    private $db_user = 'username';
    private $db_pass = 'password';
    private $db_name = 'dbname';
    private $con = mysqli_connect($this->host, $this->db_user, $this->db_pass, $this->db_name) or die('failed to connect');

    public function valid_login($login_user, $login_pass){

        if(isset($login_user) && isset($login_pass) && ($login_user !== "") && ($login_pass !== "")){
            include('./includes/functions.php');

            $clean_lu = mysqli_real_escape_string($this->con, $login_user);
            $clean_lp = mysqli_real_escape_string($this->con, $login_pass);

            $login_query = "SELECT hash FROM users WHERE user_name='$clean_lu'";
            $login_results = mysqli_query($this->con, $login_query) or die('bad query');
            $login_hash = mysqli_fetch_array($login_results);

            if(isset($login_hash[0])){
                if(check_hash($clean_lp, $login_hash[0])){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }

    mysqli_close($this->con);   
}
?>

I keep looking at that line and I can’t find any bad syntax.

I apologize if this is a really simple solution that’s right in front of my face, I’m pretty new at this whole developer thing.

  • 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-19T13:17:45+00:00Added an answer on May 19, 2026 at 1:17 pm

    You cannot initialize object’s member with result of function call. Perform this in your constructor instead.

    class Db
    {
        private $con;
    
        public function __construct()
        {
            $this->con = mysqli_connect($this->host, $this->db_user, $this->db_pass, $this->db_name) or die('failed to connect');
        }
        ...
    }
    

    Also, where is mysqli_close($this->con); located? It doesn’t belong to any of methods.

    Btw, any reason to close connection manually at all?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create my own custom validator class that I can check the
Here's a very simple example of what I'm trying to get around: class Test(object):
I'm trying to understand the IntentService class so that I can build my own
I'm trying to create my own class template for Visual Studio called Public Class.
I'm trying to create my own Vector2D class, similar to that of XNA's, to
I'm trying to create my own class to manage matrixes, but I'm a newbie
Im trying to create my own custom tag so i can display or hide
I'm trying to create my own templated List class as a practice excercise. I
I'm trying to create my own error window for the project I am working
I am trying to create my own torrent tracker but dont know how to

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.