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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:24:16+00:00 2026-06-05T15:24:16+00:00

I’m creating some basic OO scripts using MySQLi and getting an Undefined Method error

  • 0

I’m creating some basic OO scripts using MySQLi and getting an Undefined Method error when I use any of stmt_init(), prepare() or query() … Also getting an error with connect_errno(). I DO know that mysqli extension is enabled (uncommented) in my php.ini and phpinfo() has both mysqli and mysqlnd enabled … So not sure why I can’t access the methods/properties. The error I’m getting is:
Fatal error: Call to undefined method mysqli::connect_error()

class db {
    public $host = 'localhost';
    public $username = 'root';
    public $password = '';
    public $database = 'molecule';
    public $mysqli = '';
    function __construct() {
        $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
    return $this->mysqli;
    }   
}

class nodeModel {
    function __construct() {
        $this->mysqli = new db;
        if($this->mysqli->connect_error()){ printf("Database Connection failed: %s\n", $this->mysqli->connect_error()); }
    }

    function insertNode() {
        $this->insert = $this->mysqli->stmt_init();
        $this->insert->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->insert->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->insert->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->insert->close();
        print_r($this->insert_id);
        return $this->insert_id;
    }

So, to get the insertNode() method to work … these are the other methods I put in the nodeModel class

public function setNodeName($value) { $this->nodeName = $value; }
public function setNodeLink($value) { $this->nodeLink = $value; }
public function setNodeComment($value) { $this->nodeComment = $value; }
public function getNodeName() { return $this->nodeName; }
public function getNodeLink() { return $this->nodeLink; }
public function getNodeComment() { return $this->nodeComment; }

public $insert_id;
function insertNode($nodeName, $nodeLink, $nodeComment) {
        $this->mysqli->stmt_init();
        $this->mysqli->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->mysqli->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->mysqli->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->mysqli->close();
        print_r($this->insert_id);
        return $this->insert_id;
    }

How do I pass the variables into the method? I’m trying this …

$connect = new db();
$db = new nodeModel($connect);
$db->setNodeName('My Node Title');
$db->setNodeLink('My Node Link');
$db->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
$db->insertNode($db->getNodeName(), $db->getNodeLink(), $db->getNodeComment());

But that’s not working. My confusion is really about OO scope inside of class methods … I’m not sure what I’m I’m supposed to be passing.

  • 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-05T15:24:17+00:00Added an answer on June 5, 2026 at 3:24 pm

    Try this, with dependency injection pass the connection object to the model:

    <?php 
    class db{
        protected $mysqli;
        function __construct($host,$username,$password,$database) {
            if(!$this->mysqli instanceof mysqli){
                $this->mysqli = new mysqli($host, $username, $password, $database);
                if ($this->mysqli->connect_errno) {
                    die("Failed to connect to MySQL: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error);
                }
            }
        }
    }
    
    class nodeModel{
        function __construct($connection) {
            $this->mysqli = $connection;
        }
    
        function status() {
            return print_r($this,true);
        }
    }
    //Create the database object
    $connect = new db('localhost','root','password','db');
    
    //Inject the database object into the model
    $db = new nodeModel($connect);
    
    //Example method inside the model class
    print_r($db->status());
    /*
    nodeModel Object
    (
        [mysqli] => db Object
            (
                [mysqli:db:private] => mysqli Object
                    (
                        [affected_rows] => 0
                        [client_info] => mysqlnd 5.0.8-dev - 20102224 - $Revision: 310735 $
                        [client_version] => 50008
                        [connect_errno] => 0
                        [connect_error] => 
                        [errno] => 0
                        [error] => 
                        [field_count] => 0
                        [host_info] => localhost via TCP/IP
                        [info] => 
                        [insert_id] => 0
                        [server_info] => 5.5.16
                        [server_version] => 50516
                        [sqlstate] => 00000
                        [protocol_version] => 10
                        [thread_id] => 28
                        [warning_count] => 0
                    )
    
            )
    
    )
    */
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.