Code: Select all
$mySqli = $this->openConnection();
$stmnt = $mySqli->prepare("SHOW COLUMNS FROM ?");
Thanks
Moderators: egami, macek, gesf
Code: Select all
$mySqli = $this->openConnection();
$stmnt = $mySqli->prepare("SHOW COLUMNS FROM ?");
Code: Select all
<?php
class baseDal {
function __construct($host="", $user="", $password="", $database="")
{
if (empty($host)
|| empty($user)
|| empty($password)
|| empty($database))
{
$this->host = "localhost";
$this->user = "root";
$this->password = "";
$this->database = "db1";
}
else
{
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
}
}
protected function openConnection() {
$mySqli = new mysqli($this->host, $this->user, $this->password, $this->database);
return $mySqli;
}
}
Code: Select all
class dbOperations extends baseDal {
function __construct($host='', $user='', $password='', $database='') {
parent::__construct($host, $user, $password, $database);
}
function extractFields ($table){
$mySqli = $this->openConnection();
$stmnt = $mySqli->prepare("SHOW COLUMNS FROM ?"); //HERE THE ERROR APPEARS
$stmt->bind_param('s', $table);
$result = $stmnt->execute();
if (!$result)
{
return false;
}
$stmnt->bind_result($field, $type, $null, $key, $default,$extra);
while ($stmnt->fetch()) {
$field = array('Field'=>$field,
'Type'=>$type);
$fields[] = $field;
}
$stmnt->close();
return $fields;
}
}
Code: Select all
// Incorrect
$stmnt = $mySqli->prepare("SHOW COLUMNS FROM ?");
$stmt->bind_param('s', $table);
// Correct
$stmnt = $mySqli->prepare("SHOW COLUMNS FROM ?");
$stmnt->bind_param('s', $table);
Code: Select all
$stmnt = $mySqli->prepare("SHOW COLUMNS FROM " . $table);
You might try something like this instead:[Parameter Markers (?)] are legal only in certain places in SQL statements. For example, they are allowed in the VALUES() list of an INSERT statement (to specify column values for a row), or in a comparison with a column in a WHERE clause to specify a comparison value.
However, they are not allowed for identifiers (such as table or column names)...
-- The PHP Group. PHP: mysqli::prepare - Manual. Updated 2012-06-08.
Code: Select all
$stmnt = $mySqli->prepare(sprintf("SHOW COLUMNS FROM `%s`;",
strtr($table,array('`'=>''))));