【问题标题】:MariaDB statement not compatible with MySQLMariaDB 语句与 MySQL 不兼容
【发布时间】:2017-07-07 06:16:58
【问题描述】:

后端不是我最强大的背景,我在从 MySQL 迁移到 MariaDB 时遇到了执行我的 php 脚本的麻烦(我们的服务器提供商必须这样做)。 这是我在我的 php 脚本中使用 MySQL 5.6 的函数,但它不适用于 MariaDB(10.1.22-MariaDB-cll-lve - MariaDB 服务器):

public function getPostByName($postName) {
        $returnValue = array();
        $sql = "SELECT * FROM posts WHERE post_description LIKE ?";
        $statement = $this->conn->prepare($sql);
        if (!$statement)
            throw new Exception($statement->error);

        $postName = "%".$postName."%";
        $statement->bind_param("s", $postName);
        $statement->execute();

        $result = $statement->get_result();
        while($row = $result->fetch_array(MYSQLI_ASSOC)) {
            array_push($returnValue, $row);
        }
        return $returnValue;
}

我已经尝试了大多数选项,但没有一个有效(一个例子:php mysqli prepared statement LIKE)。有什么建议吗?

【问题讨论】:

  • 你遇到了什么错误?
  • 我没有收到任何错误......完全没有!没什么..
  • print_r($result) 让我们知道什么是 o/p
  • 还是什么都没有……
  • 如果您直接在 Maria DB 控制台中键入查询,它可能会起作用?

标签: php mysql mariadb backend


【解决方案1】:

已解决:

public function getPostByName($postName){

    $returnValue = array();
    $sql = "SELECT * FROM posts WHERE post_description LIKE '%".$postName."%'";
    $result = $this->conn->query($sql);

    if($result != nill && (mysqli_num_rows($result) >= 1)){
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
            array_push($returnValue, $row);
        }
    }
    return $returnValue;
}

【讨论】:

  • 所以如果 postName 是 Steve';delete from posts;-- 会伤害你吗?
  • 这就是所谓的“sql注入”;真恶心。
  • $result != nill 应该是$result === false
猜你喜欢
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 2019-10-09
相关资源
最近更新 更多