gpt4 book ai didi

php - 如何知道名称是指 php 脚本中的 mysql 表还是 mysql View

转载 作者:IT王子 更新时间:2023-10-28 23:53:36 25 4
gpt4 key购买 nike

我可以像这样选择数据库中的所有表

$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}

以下代码填充 $return 变量,该变量可用于备份数据库。

  foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);

$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}

我的数据库有两个 mysql View 。即使对于我需要避免的 mysql View ,上面的代码也会生成“INSERT INTO ....”字符串。因此,在启动“for 循环”以生成“INSERT INTO..”值之前,我需要检查 $table 是否实际上是一个 mysql 表或 View 。如何判断一个名字是指mysql表还是mysql View ?

最佳答案

SHOW [FULL] TABLES

支持 FULL 修饰符,以便 SHOW FULL TABLES 显示第二个输出列。第二列的值是表的 BASE TABLE 和 View 的 VIEW

关于php - 如何知道名称是指 php 脚本中的 mysql 表还是 mysql View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com