所以我正在尝试制作备用课程,这是我到目前为止所做的。问题是$ tbl_data为空。我做错了什么。
与数据库的连接成功。
如果没有'echo $ tbl_data','$ current_table - current table'输出是正确的,但是如果使用'echo $ tbl_data',则只显示第一个表(尝试备份两个表开始)。 / p>
class mBackup{
private $_connection = ""; //db connection var
private $output = ""; //sql output
private $tbl_data = "";
private $tbl_row = "";
private $nfields = "";
private $create_table_query = "";
private $create_table_output = "";
public function __construct($dbhost,$dbname,$dbuser,$dbpassword){
$this->_connection = new mysqli($dbhost,$dbuser,$dbpassword,$dbname);
//possible connection error
if($this->_connection->connect_errno){
echo "Failed to connect to the DB";
}
else{
echo "Connected<br />";
}
}
public function backup_db(){
//get the table names from the DB and store in an array
$result = $this->_connection->query("SHOW TABLES");
//get the TABLE names
while($row = $result->fetch_row())
{
$table_names[] = $row[0];
}
//For each table
foreach($table_names as $current_table)
{
echo $current_table." - current table<br />"; //debug
$tbl_data = "";
$tbl_row = "";
$nfields = "";
$create_table_query = "";
$create_table_output = "";
//SELECT Everything from the table in use
$query = $this->_connection->prepare("SELECT * FROM ?");
$query->bind_param('s', $current_table);
$query->execute();
$query->bind_result($tbl_data);
$query->fetch();
echo $tbl_data."<br/>";
}
}
答案 0 :(得分:0)
尝试类似:
while ($query->fetch()) {
echo $tbl_data;
}
看看能不能得到任何东西。从我所知道的那一点开始,bind_result将结果集中的列绑定到变量。如果你的表有5列,你应该有bind_result($ var1,$ var2,$ var3,$ var4,$ var5),但由于你的列数会根据表而改变,我不知道bind_result是否会给你你需要的东西。
尝试在每个循环后关闭准备好的语句
$query->close();
或重置。
$query->reset()
答案 1 :(得分:0)
$select = sprintf("SELECT * FROM `%s`", $current_table);
$result = $this->_connection->query($select);