为什么不准备语句执行?

时间:2016-10-20 13:33:23

标签: php mysql

我正在尝试将bind语句插入表中。我所知道的是,建立连接但是准备声明不起作用。我做错了吗?

function parseXML(xml) {
  var onlineOrderObj = {}; //hold information
  var onlineOrderElement = xml.getElementsByTagName("onlineOrder")[0];
  onlineOrderObj.month = onlineOrderElement.getAttribute("month");
  onlineOrderObj.productList = []; //create array

  var productElements = onlineOrderElement.getElementsByTagName("product");
  console.log(productElements);
  for (var i = 0; i < productElements.length; i++) {
    var product = {}; //create index object

    product.code = productElements[i].getElementsByTagName("code")[0].childNodes[0].nodeValue;

    product.unitPrice = Number(productElements[i].getElementsByTagName("unitPrice")[0].childNodes[0].nodeValue); // these were named wrong
    product.lastMonth = Number(productElements[i].getElementsByTagName("lastMonth")[0].childNodes[0].nodeValue);
    product.thisMonth = Number(productElements[i].getElementsByTagName("thisMonth")[0].childNodes[0].nodeValue);

    onlineOrderObj.productList.push(product); //object into array
  }
  return onlineOrderObj;
}

3 个答案:

答案 0 :(得分:1)

您需要使用MySQLi连接而不是您自己的对象。

<?php
class connDb{
    private $link;
    private $host;
    private $user;
    private $pass;
    private $dbname;
    public function __construct($host, $user, $pass, $dbname){
        $this->host=$host;
        $this->user=$user;
        $this->pass=$pass;
        $this->dbname=$dbname;
        $this->link=mysqli_connect($this->host, $this->user, $this->pass, $this->dbname)
            OR
        die("Not connected to MySQL");
    }
    public function getLink() {
        return $this->link;
    }
}

$conn1=new connDb("localhost", "root", "", "dbind");
$xyz=$conn1->getLink()->prepare("INSERT INTO table1 VALUES(?, ?, ?)");
$x="vikas";
$y="vikas@gmail.com";
$z="123n";
$xyz->bind_param('sss', $x, $y, $z);

$xyz->execute();
?>

答案 1 :(得分:1)

您的逻辑错误:您的connDb类不是mysqli对象,它只包含一个mysqli对象的私有属性。

你需要一个吸气剂来使用它。

例如:

<?php
class connDb{
    private $link;
    private $host;
    private $user;
    private $pass;
    private $dbname;

    public function __construct($host, $user, $pass, $dbname){
        $this->host=$host;
        $this->user=$user;
        $this->pass=$pass;
        $this->dbname=$dbname;
        $this->link=mysqli_connect($this->host, $this->user, $this->pass, $this->dbname)
            OR
        die("Not connected to MySQL");
    }

    public function getLink() {
         return $this->link;
    }
}

$conn1=new connDb("localhost", "root", "", "dbind");
$xyz=$conn1->getLink()->prepare("INSERT INTO table1 VALUES(?, ?, ?)");
...

答案 2 :(得分:0)

您错过了返回$this->link;

请在connDb班级

中添加新功能
public function getDB() {
   return $this->link;
}

所以,最后请遵循以下代码:

<?php
class connDb{
    private $link;
    private $host;
    private $user;
    private $pass;
    private $dbname;
    public function __construct($host, $user, $pass, $dbname){
        $this->host=$host;
        $this->user=$user;
        $this->pass=$pass;
        $this->dbname=$dbname;
        $this->link = mysqli_connect($this->host, $this->user, $this->pass, $this->dbname) OR die("Not connected to MySQL");
    }

    public function getDB() {
        return $this->link;
    }
}
?>

<?php
$conn1=new connDb("localhost", "root", "mysql", "db_mvc");
$xyz=$conn1->getDB()->prepare("INSERT INTO category (name, email, password) VALUES(?, ?, ?)");
$x="vikas";
$y="vikas@gmail.com";
$z="123n";
$xyz->bind_param('sss', $x, $y, $z);
$xyz->execute();
?>