准备声明填写

时间:2015-09-25 19:51:58

标签: php mysql oop prepared-statement

所以我试图让一个类(对于mysqli操作)尽可能多地重用其他类(使用不同的表和参数)。来自主类的片段:

class sql
{
    private $sql;
    public function __construct()
    {
        $this->sql = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
        if ($this->sql->connect_errno > 0)
        {
            die('Unable to connect to database: ' .$this->sql->connect_error);
        }
        elseif (!$this->sql->set_charset("utf8"))
        {
            printf('Error loading character set utf8: ' .$this->sql->error);
        }
    }
    public function newEntry($table, $entryString, $valueString, $typeString, $dataString)
    {
        $statement = $this->sql->prepare("INSERT INTO $table($entryString) VALUES ($valueString)");
        $statement->bind_param($typeString, $dataString);
        $statement->execute();
        $statement->close();
        $this->sql->close();
    }
}

这是我的另一个类,我试图将变量传递给main函数newEntry():

class user extends sql
{
    protected $table = 'foo';

    public function newUser($name, $surname)
    {
        $entryString = 'name, surname';
        $valueString = '?, ?';
        $typeString = 'ss';
        $dataString = '$name, $surname';
        parent::newEntry($this->table, $entryString, $valueString, $typeString, $dataString);
    }
}

因此,当我尝试执行此操作时,我得到一个“警告:mysqli_stmt :: bind_param():类型定义字符串中的元素数与...中的绑定变量数不匹配”。我理解这是因为我的$ dataString就像一个条目:“John,Doe”。有没有办法让它像许多单独的参数一样,而不仅仅是一个字符串?

0 个答案:

没有答案