0

所以我正在尝试做一个备用课程,这就是我到目前为止所拥有的。问题是 $tbl_data 为空。我究竟做错了什么。

与数据库的连接成功。

如果没有'echo $tbl_data','$current_table - current table' 输出是正确的,但如果使用'echo $tbl_data',则只显示第一个表(尝试备份两个表开始)。

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/>";

        }
}
4

2 回答 2

0

尝试类似:

while ($query->fetch()) {
     echo $tbl_data;
}

看看能不能帮到你。据我所知,bind_result 将结果集中的列绑定到变量。如果你的表有 5 列,你应该有 bind_result($var1, $var2, $var3, $var4, $var5) 但由于你的列数会根据表而改变,我不知道 bind_result 是否会给你你需要的。

尝试在每个循环后关闭准备好的语句

$query->close();

或重置。

$query->reset()
于 2012-07-26T15:34:53.793 回答
0

你不能用吗?为表名。有关标记的允许位置,请参见http://www.php.net/manual/en/mysqli.prepare.php中的第二个注释。因此,您必须通过普通变量插值构造查询:

$select = sprintf("SELECT * FROM `%s`", $current_table);
$result = $this->_connection->query($select);
于 2012-07-26T16:01:28.827 回答