0

I have been building an xml parser what should red all xml files from a folder and after parsing theme should save data in mysql table. After all I'm getting the following error. I'm not sure what I'm doing wrong and how should i debug it.

Warning: mysqli::prepare(): Couldn't fetch mysqli in E:\xampp\htdocs\XML_Parser\index.php on line 38

my code

class XMLFeeds{

    public $obj;

    protected $db_connect;

    function __construct(){     
        // Read feeds and pass it to parser
        $this->db_connect = @new mysqli('loalhost', 'root', '', 'test');
        foreach(glob("feeds/*xml") as $filename) {
            $obj = $this->parsing_feed(file_get_contents($filename, FILE_TEXT) );
            $this->saveFeed($obj);
        }   
    }

    function parsing_feed($feed){       
        return simplexml_load_string($feed);        
    }   

    function saveFeed($obj){
        foreach ($obj as $row){

                $stmt = $this->db_connect->prepare("INSERT INTO tbl 
                        ( id,
                          prod_name,
                          category,
                          description,
                          image_url,
                          keywords,
                          sku,
                          target_url,
                          price,
                          warranty,
                          shipping_costs,
                          impressionurl,
                          lastupdated ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

                if ($stmt === FALSE) {
                    die($mysqli->error);
                }

                $stmt -> bind_param("ssi",
                         $row->name,
                         $row->advertisercategory,
                         $row->description,
                         $row->imageurl,
                         $row->keywords,
                         $row->sku,
                         $row->buyurl,
                         $row->price,
                         $row->standardshippingcostm,
                         $row->impressionurl,
                         $row->lastupdated );
                $stmt ->execute();
        }
    }
}

$parse = new XMLFeeds();
4
  • 1
    You shouldn't suppress errors on the database connection. Commented Feb 13, 2013 at 21:28
  • 1
    ... especially as the error might be right there (loalhost) Commented Feb 13, 2013 at 21:29
  • yepp to tired, typing error Commented Feb 13, 2013 at 21:30
  • sorry for this issue and thank you for your help Commented Feb 13, 2013 at 21:36

1 Answer 1

1

this happened because the connection is not established successfully with the database because the localhost is spelled wrong. you are omitting the errors during instantiate mysqli class by using @new.

try to remove the @ and check if the connection is not establish to through an exception otherwise it will proceed with code execution .

    <?php
    $this->db_connect = new mysqli('localhost', 'root', '', 'zanox');
    if($this->db_connect->connect_error){
       throw new Exception('unable to connect, '. $this->db_connect->connect_error);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.