0

I am new to classes in PHP.

class metacountry
{
  public $mysqli;
  function metacountry()
  {

    global $config;
    $mysqli=new mysqli($config['DBHostName'],$config['DBUserName'],$config['DBPassword'],$config['DBName']);
   $mysqli->set_charset("utf8");
   if (mysqli_connect_errno()) {

      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();

    }

  }

  function InsertCountry()
  {

    $stmt=$mysqli->prepare("insert into tbl_metacountry values(?,?,?,?,?)");
    $stmt->bind_param('sssss', $country, $meta_title, $meta_keywords, $meta_description, $active);
    $country=addslashes(addcslashes($mysqli->real_escape_string($_POST['country'], "%_")));
    $meta_title=addslashes(addcslashes($mysqli->real_escape_string($_POST['meta_title'], "%_")));
    $meta_keywords=addslashes(addcslashes($mysqli->real_escape_string($_POST['meta_keywords'], "%_")));
    $meta_description=addslashes(addcslashes($mysqli->real_escape_string($_POST['meta_description'], "%_")));
    $meta_active=addslashes(addcslashes($mysqli->real_escape_string($_POST['active'], "%_")));
    $stmt->execute();
    $stmt->close();

  }

 }  

It throws an error:

Fatal error: Call to a member function prepare() on null in /var/www/html/country.php

halfer
  • 19,824
  • 17
  • 99
  • 186
Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77

2 Answers2

3

You have to use $this to refer to class properties.

Your connection:

$mysqli=new mysqli($config['DBHostName'],$config['DBUserName'],$config['DBPassword'],$config['DBName']);

Should be,

$this->mysqli[...]

Additional Information

Instead of using global you should define the database settings within the class to make it more OO.

Reading Material

Class Properties

Script47
  • 14,230
  • 4
  • 45
  • 66
  • thanks, Actually I am new to classes Actually I want to create a mysqli object in the constructor method and use it in other methods in the class. and if you could edit my question and post here that will be very help ful – Sugumar Venkatesan Dec 17 '15 at 11:56
  • @SugumarVenkatesan then I think you should start from the beginning, don't jump in to database classes. Learn about classes in general and use trial & error to get used to the features involved in classes. Please accept the answer if it helped you. – Script47 Dec 17 '15 at 11:57
  • I keep getting "Class 'CronClass\mysqli' not found" when using either method. – user2924019 Jul 30 '18 at 14:51
  • @user2924019 In the future you should ask a new question however, before `new mysqli` put a backslash so `new \mysqli(...);`, that will check the global namespace for the mysqli class instead of your class's namespace. – Script47 Jul 30 '18 at 14:52
  • My hero! I was going to create a new question but the anxiety of getting downvoted and my account closed from asking questions people think already have answers is too much. The \ worked, I'm confused as to why all the examples I found including on this question don't seem to need the \. Thanks. – user2924019 Jul 30 '18 at 14:57
  • In your class you will have used `namespace blah` or something along those lines, therefore your class is no longer in the global namespace, all the classes defined without the `namespace` keyword are in the global namespace to by knowledge. – Script47 Jul 30 '18 at 15:06
  • @user2924019 BTW, I typed in Google '*class mysqli not found'* and found [this](https://stackoverflow.com/questions/666811/fatal-error-class-mysqli-not-found) question and if you scroll down, someone mentioned the namespace issue. Google is your friend. – Script47 Jul 30 '18 at 15:07
  • @Script47 I did see that question, but I didn't see the answer about the namespace issue. – user2924019 Jul 31 '18 at 11:37
1

I hope the following makes sense - it's not tested but looks more or less correct. The db connection is created in the public constructor function and is referenced using $this->conn for clarity - though you could of course rename that to whatever you wanted.

<?php
    class metacountry{
        private $conn;
        private $config;

        public function __construct($config=array()){
            /* initialise actual values at runtime */
            $this->config=(object)array_merge(array(
                'DBHostName'    =>  false,
                'DBUserName'    =>  false,
                'DBPassword'    =>  false,
                'DBName'        =>  false
            ),$config);


            $this->conn=new mysqli( $this->config->DBHostName, $this->config->DBUserName, $this->config->DBPassword, $this->config->DBName );
            $this->conn->set_charset("utf8");

            if( $this->conn->connect_errno ) {
                printf( "Connect failed: %s\n", $this->conn->connect_error );
                exit();
            }       
        }



        function InsertCountry(){
            $stmt=$this->conn->prepare("insert into `tbl_metacountry` values (?,?,?,?,?)");
            $stmt->bind_param('sssss', $country, $meta_title, $meta_keywords, $meta_description, $active );

            $country=addslashes( addcslashes( $this->conn->real_escape_string( $_POST['country'], "%_" ) ) );
            $meta_title=addslashes( addcslashes( $this->conn->real_escape_string( $_POST['meta_title'], "%_" ) ) );
            $meta_keywords=addslashes( addcslashes( $this->conn->real_escape_string( $_POST['meta_keywords'], "%_" ) ) );
            $meta_description=addslashes( addcslashes( $this->conn->real_escape_string( $_POST['meta_description'], "%_" ) ) );
            $meta_active=addslashes( addcslashes( $this->conn->real_escape_string( $_POST['active'], "%_" ) ) );

            $stmt->execute();
            $stmt->close();
        }
    } 



    /* example */
    $config=array(
        'DBHostName'=>'localhost',
        'DBUserName'=>'root',
        'DBPassword'=>'password',
        'DBName'    =>'mydb'
    );
    $meta=new metacountry( $config );
    if( $_SERVER['REQUEST_METHOD']=='POST' ) $meta->InsertCountry();
    $meta=null;
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46