0

我一直在构建一个 xml 解析器,应该从一个文件夹中删除所有 xml 文件,并且在解析主题后应该将数据保存在 mysql 表中。毕竟我收到以下错误。我不确定我做错了什么以及我应该如何调试它。

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

我的代码

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 回答 1

1

发生这种情况是因为 localhost 拼写错误,未成功建立与数据库的连接。您在使用@new 实例化 mysqli 类期间省略了错误。

尝试删除@并检查是否没有通过异常建立连接,否则它将继续执行代码。

    <?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);
    }
于 2013-02-13T21:40:05.750 回答