【问题标题】:mysqli prepared statments classmysqli 准备好的语句类
【发布时间】:2015-04-30 00:25:07
【问题描述】:

我正在为 mysql 语句构建我的第一个类,以使我自己的查询更容易。基本上我们会这样做。

Query::set_config($host,$username,$password);

if( Query::is_database("database_name") ) {
    Query::get_results("SELECT * FROM REGISTRY WHERE `name`=? AND `last`=?",[
        "s"=>"firstname",
        "s"=>"lastname"
    ]);
}

我的问题是prepared和not,以及procedural和OO之间的细微差别。

这是get_results背后的底层代码

/*Load Results from SQL Query*/ 
public static function results($sql,$prepared=null) {
    if($is_configurated) {
        self::connect();
        $rows = array();
        if($prepared === null) {
            $query = mysqli_query(self::$conn,$sql);
            if(!$query) 
                echo mysqli_error(self::$conn);

            while($r = mysqli_fetch_array($query)) {
                $rows[] = $r;
            }
        }
        else 
        {
            $stmt = mysqli_prepare(self::$conn,$sql);
            /*
                i   corresponding variable has type integer
                d   corresponding variable has type double
                s   corresponding variable has type string
                b   corresponding variable is a blob and will be sent in packets
            */
            foreach($prepared as $type=>$param) {
                if(strlen($type) > 1) {
                    self::$errors[]="Only one type is allowed";
                    continue;
                } else {
                    mysqli_stmt_bind_param($stmt,$type,$param);
                }
            }

            /*STUCK HERE NOW*/
            mysqli_stmt_execute($stmt);
            while (mysqli_stmt_fetch($stmt)) {
                $rows[] = $r;
            }

        }
        self::disconnect();
        return $rows;
    } else return "NO CONFIGURATED CONNECTION";
}

我写了我卡住的地方,这就像普通的mysqliwhilemysqli_fetch_row 还是什么。 PHP.net 文档不是很流畅,也不是很简单。我只需要帮助来做这件事,看看我的代码中是否有任何小缺陷。我需要从结果中返回行。

这是正确的吗?

        /*Load Results from SQL Query*/ 
    public static function results($sql,$prepared=null) {
        if($is_configurated) {
            self::connect();
            $rows = array();
            if($prepared === null) {
                $query = mysqli_query(self::$conn,$sql);
                if(!$query) 
                    echo mysqli_error(self::$conn);

                while($r = mysqli_fetch_array($query)) {
                    $rows[] = $r;
                }
            }
            else 
            {
                $stmt = mysqli_prepare(self::$conn,$sql);
                /*
                    i   corresponding variable has type integer
                    d   corresponding variable has type double
                    s   corresponding variable has type string
                    b   corresponding variable is a blob and will be sent in packets
                */
                foreach($prepared as $type=>$param) {
                    if(strlen($type) > 1) {
                        self::$errors[]="Only one type is allowed";
                        continue;
                    } else {
                        mysqli_stmt_bind_param($stmt,$type,$param);
                    }
                }

                /*STUCK HERE NOW*/
                mysqli_stmt_execute($stmt);
                $stored = mysqli_store_result($stmt);
                if( count($stored) >= 1) {
                    while ($data = mysqli_stmt_fetch($stmt)) {
                        $rows[] = $data;
                    }
                }

            }
            self::disconnect();
            return $rows;
        } else return "NO CONFIGURATED CONNECTION";
    }

【问题讨论】:

  • mysqli 不支持命名占位符。
  • ? 然后是对的,因为我知道它要么是 PDO 要么被命名为占位符
  • 想我现在有了。

标签: php class mysqli prepared-statement


【解决方案1】:

如果您想在准备好的语句上使用 mysqli 函数,请查看mysqli_stmt_get_result 函数。但是,此功能仅适用于 mysqlnd 驱动程序。

【讨论】:

  • @EasyBB 我猜这是因为您在mysqli 语句中使用了? 而没有将? 替换为实际参数。
  • 是的,我认为我必须这样做。只是想确保我使用的是准备好的,这样更安全。
  • @EasyBB 使用 mysqli 时,使用 mysqli_real_escape 字符串作为字符串数据,使用 intval 作为 int 数据,您是安全的。
  • 这并不总是失败证明,这就是我编写准备好的语句类的原因
  • @EasyBB 失败证明是什么意思?
猜你喜欢
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 2011-03-07
  • 2014-06-11
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多