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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:25:42+00:00 2026-06-18T07:25:42+00:00

I am trying to pass a mysqli connection to a class for it to

  • 0

I am trying to pass a mysqli connection to a class for it to use prepared statements. However, I am missing something and it is not working. What a i doing wrong?

The following code outputs “no”

index.php

$db_host = 'localhost';
$db_name = 'my_database';
$db_user = 'my_user';
$db_password = 'my_password';

$con = new mysqli($db_host,$db_user,$db_password,$db_name);
if (mysqli_connect_errno())
{
    die(mysqli_connect_error()); 
}

$obj = new Obj( $con );

obj.php

Class Obj
{
    public function __construct( mysqli $con )
    {
        $sql = "SELECT * FOM myTable";
        if($con->prepare( $sql ))
        {
            echo "yes";
        }
        else
        {
            echo "no";
        }        
    }
}
  • 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-06-18T07:25:44+00:00Added an answer on June 18, 2026 at 7:25 am

    If you had failed to pass a mysqli connection to your class, you’d be getting a fatal error on this line:

    public function __construct( mysqli $con )
                                 ^^^^^^
    

    But not only the constructor starts running fine, you can even access a class method:

    if($con->prepare( $sql ))
    

    To sum up, all that happens is that mysqli::prepare() returns false because there was an error while preparing the SQL statement. I’m not familiar with mysql but you can probably get the exact error message with mysqli::$error_list or mysqli::$error:

        if($con->prepare( $slq ))
        {
            echo "yes";
        }
        else
        {
            echo "no: " . $con->error;
        } 
    

    If SELECT * FOM myTable is the actual query, I suppose it’s a simple syntax error since you’ve misspelt FROM.

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

Sidebar

Related Questions

Trying to pass something to something else. Says undefined. Not sure how or why.
I'm currently trying to pass a DB connection as follows: class Test { public
Possible Duplicate: Can't pass mysqli connection in session in php I'm trying to store
I am trying pass class as value in hashmap. I need to get the
I am trying to pass the fullcalendar events to my controller via Json, however
I'm trying to create a database connection class that all my models will extend
I'm trying to dynamically add class properties into an PDO prepared statement. To achieve
Hey guys I'm doing this wrong again I'm sure, but I'm trying to instantiate
I'm trying to make a Helper class where you simply pass the driver and
I'm trying to create a simple to use singleton class to connect to mysql

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.