0

Can anyone help me

This works fine ::

            $stmt = $this->conn->prepare("SHOW TABLES FROM db LIKE 'xyz'"); //tablename hardcoded
            $stmt->execute();
            $rows = $stmt->fetch(PDO::FETCH_ASSOC);  //fetchAll
            

However if I try to bind my table name, as it will be dynamic in my script ::

            $stmt = $this->conn->prepare("SHOW TABLES FROM db LIKE :tbl"); //tablename hardcoded
            $tbl = "xyz";
            $stmt->bindParam(":tbl", $tbl, PDO::PARAM_STR);
            

This gives error,

            $stmt = $this->conn->prepare("SHOW TABLES FROM cens LIKE ?");
            $tbl = "xyz";
            $stmt->bindParam(1, $tbl, PDO::PARAM_STR);

This also gives error,

Error is

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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' in /opt/lampp/htdocs/PDO/test.php:132 Stack trace:

0 /opt/lampp/htdocs/PDO/test.php(132): PDO->prepare('SHOW TABLES FRO...')

I tried with quotes, both in query and in bind variable, but it didn't work

I want to bind the table name,

Community
  • 1
  • 1
Hytool
  • 1,358
  • 1
  • 7
  • 22
  • Whats wrong with you guys, why am I given negative point always, I always research first and then ask. my last questing was also given negative point. I havent found any solution for both of the questions. just given the negative points. – Hytool May 18 '15 at 10:59

1 Answers1

0

Try the following:

$string = 'xyz';
$somethingelse ="%".$string."%";
$sth =$dbh->prepare("select * from tablename WHERE something LIKE :somethingelse")
$sth->bindParam(":somethingelse",$somethingelse)

Another alternative would be to do:

$string = 'xyz';
$somethingelse ="%".$string."%";
$sth =$dbh->prepare("select * from tablename WHERE something LIKE CONCAT(:string, '%')")
$sth->bindParam(":string",$string)

Either of these methods should work for you.

The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
  • That didnt work either. I just want to know that the table exists in other database or not. dont want to use the SELECT clause. – Hytool May 18 '15 at 10:55
  • These don't make much sense and there are unused variables.... and it doesn't answer the OP's question. – ashleedawg Aug 31 '22 at 23:33