【问题标题】:bind_param for php pdo for select用于选择的 php pdo 的 bind_param
【发布时间】:2019-07-31 19:33:33
【问题描述】:

我在 php pdo 中创建了删除语句。我需要知道我的代码的 bind_param 查询。我使用了一个无法正常工作的 bind_param。

PHP7

public function deleteRecord($table,$pk,$id){
        if($table == "categories"){
            $pre_stmt = $this->con->prepare("SELECT ".$id." FROM categories WHERE parent_cat = ?");
            $pre_stmt->bind_param("i",$id);
            $pre_stmt->execute();
            $result = $pre_stmt->get_result() or die($this->con->error);
            if ($result->num_rows > 0) {
                return "DEPENDENT_CATEGORY";
            }else{
                $pre_stmt = $this->con->prepare("DELETE FROM ".$table." WHERE ".$pk." = ?");
                $pre_stmt->bind_param("i",$id);
                $result = $pre_stmt->execute() or die($this->con->error);
                if ($result) {
                    return "CATEGORY_DELETED";
                }
            }

【问题讨论】:

  • bind_param 是 mysqli api 它是 bingParam 用于 PDO。 get_result() php.net/manual/en/pdostatement.bindparam.php 也一样
  • 看来您需要阅读 PDO 教程才能了解正确的使用方法。
  • 您的 SELECT 有 WHERE parent_cat = ?,而您的 DELETE 有 WHERE ".$pk." = ?
  • 这都是MySQLi的代码,没有多大意义。你确定你发布了正确的代码吗?

标签: php pdo


【解决方案1】:

我在上面纠正了,但在下面出错了 公共函数 getAllRecord($table){

    $result = $this->con->prepare("SELECT * FROM ".$table);
    $result->execute();
    $results = $result->fetchAll();
    $rows = array();
    if ($results->rowCount > 0) {
        while($row = $results->fetch(PDO::FETCH_ASSOC)){
            $rows[] = $row;
        }
        return $rows;
    }
    return "NO_DATA";
}

错误:注意:尝试在第 68 行的 C:\xampp\htdocs\inv_project\public_html\includes\DBOperation.php 中获取非对象的属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 2014-02-22
    • 2016-11-25
    • 2013-06-21
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多