为什么我为MySQL编写的PHP语句不起作用?

为什么我为MySQL编写的PHP语句不起作用?,php,mysql,Php,Mysql,我目前正在学习PHP和MySQL。我刚刚编写了一个处理所有MySQL流量的类,但是我遇到了一些错误 function table_exists($tablename){ // check if table exists $stmt = $this->db->prepare("SHOW TABLES LIKE '?'"); $stmt->bind_param("s", $tablename); //This is line 24. $stmt-&g

我目前正在学习PHP和MySQL。我刚刚编写了一个处理所有MySQL流量的类,但是我遇到了一些错误

function table_exists($tablename){
    // check if table exists
    $stmt = $this->db->prepare("SHOW TABLES LIKE '?'");
    $stmt->bind_param("s", $tablename); //This is line 24.
    $stmt->execute();
    $ar = $stmt->affected_rows;
    $stmt->close();
    if ($ar > 0){
        return true;
    } else {
        return false;
    }
}
这是有问题的代码,我得到的错误是

生成警告: mysqli_stmt::bind_param() [mysqli stmt.bind param]:数量 变量与的数量不匹配 中已准备语句中的参数 C:\xampp\htdocs\mail\datahandler.php 在线24

想法


谢谢

在处理准备好的语句时,无需使用引号

$stmt = $this->db->prepare("SHOW TABLES LIKE ?");

此外,您可能希望使用信息模式视图,而不是
显示表,这给了您更多的灵活性。

您还必须使用数字作为bind\u param()的第一个参数

请看这里:


对于字符串,您也可以将数组传递到execute()。

这是否意味着如果我这样做,我也不需要引号$query=“从id为“”的邮件列表中删除?”“和”。“password='password(?);在使用代码之后,我发现您的SQL语法有一个错误;检查与您的MySQL服务器版本对应的手册,以了解在第1Maybe行的“?”附近使用的正确语法。显示表格不支持LIKE子句中的准备语句。。。请尝试
从信息_schema.TABLES中选择TABLE_NAME,其中TABLE_schema=?TABLE_NAME=?
其中
TABLE_SCHEMA
是数据库的名称,
TABLE_NAME
是表的名称。好的,我没有意识到你在使用MySQLi,我假设你在使用PDO——顺便说一句,我强烈推荐PDO。
$stmt->bind_param(1, $tablename);
private function table_exists($tablename){
        // check if table exists
        $stmt = $this->db->query("SHOW TABLES");
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $arr[]=$row;
        }
        $ar=0;
        foreach($arr as $val){
            foreach($val as $value){
                if($value==$tablename) $ar=1;
            }
        }
        unset($stmt);
        if ($ar == 1){
            return true;
        } else {
            return false;
        }
    }