tags:

views:

74

answers:

2

I'm currently learning PHP and MySQL. I'm just wrote a class that handles all the MySQL traffic, but I'm encountering some errors.

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;
    }
}

This is the code with the problem, and the error i'm getting is

Generates Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\mail\datahandler.php on line 24

Ideas?

Thanks

+2  A: 

No need to use quotes when working with prepared statements.

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

Also, instead of SHOW TABLES, you might want to use information_schema views, which give you a bit more flexibility.

Mchl
Does that mean if i do this, i won't need the quote either?$query = "DELETE FROM maillist WHERE id='?' AND" . "password='PASSWORD(?)'";
ultimatebuster
After using your code, i'm getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
ultimatebuster
Maybe SHOW TABLES does not support prepared statements in LIKE clause... Try `SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?` where `TABLE_SCHEMA` is name of your database, and `TABLE_NAME` is name of your table.
Mchl
+2  A: 

You also have to use a number as first parameter for bind_param()

$stmt->bind_param(1, $tablename);

See here: http://php.net/manual/pdostatement.bindparam.php

For strings you can also just pass an array into execute().

DanMan
My $stmt is http://php.net/manual/en/mysqli-stmt.bind-param.php I'm pretty sure.
ultimatebuster
Ok, I didn't realize you're using MySQLi, i assumed you were using PDO - which I highly recommend btw.
DanMan