[英]using PHP switch control statement to list each table column and row in MySQL database
I am new to PHP. 我是PHP新手。
Please I need help with the code below.I am trying to use PHP switch statement to display table data from MySQL database based on the selection of a particular table. 请使用下面的代码获取帮助。我正在尝试使用PHP switch语句基于对特定表的选择来显示MySQL数据库中的表数据。 I have 2 PHP files (index.php and table_data.php).
我有2个PHP文件(index.php和table_data.php)。 The file index.php has a function that list all the tables from the database with onclick function to list the row and columns of a particular table.
文件index.php具有使用onclick函数列出数据库中所有表的功能,以列出特定表的行和列。 The second file table_data.php contains the switch statement but the switch statement does not list the table data when a particular table is selected, it does not list the row and columns of the table.
第二个文件table_data.php包含switch语句,但是当选择特定表时,switch语句不列出表数据,它不列出表的行和列。 I Think the problem is with the switch statement.
我认为问题出在switch语句上。 Please help and thanks in advance.
请帮助并提前致谢。 below is the table_data.php file
以下是table_data.php文件
<?php
include_once 'connect.php';
function showtable()
{
$dbname = 'Database';
echo "TABLE NAMES";
$tables = mysql_query("SHOW TABLES FROM `" . $dbname . "`");
while (list($table) = mysql_fetch_row($tables))
{
echo "<div onclick='table()' title='click to show table data'>$table</div>";
}
}
?>
<body>
<script>
function table()
{
document.getElementById('video').src='table_data.php';
}
</script>
<table width="100%" cellpadding="1" id="content">
<tr>
<td width="20%" valign="top" bgcolor="#0033FF">
<?php showtable();?>
</td>
<td valign="top" >
<iframe id="video" src="table_data.php">
</iframe>
</td>
</tr>
</table>
</div>
</body></html>
It's probably because the variable $table
is not visible inside the function showtabledata()
. 可能是因为变量
$table
在showtabledata()
函数中不可见。 So you can either import (sort of) the variable inside the function like this : 因此,您可以像这样在函数内部导入(排序)变量:
$table = '';
function showtabledata()
{
/* other code */
global $table;
/* other code */
}
or can pass the variable as an argument to the function like showtabledata($table)
as suggested by Sean, like this : 或者可以按照Sean的建议将变量作为参数传递给
showtabledata($table)
类的函数,如下所示:
$table = '';
function showtabledata($table)
{
// use $table
}
Have a look at Language variables scope 看一下语言变量范围
Please try with this code, use showtabledata($table) , pass $table through function 请尝试使用此代码,使用showtabledata($ table),通过函数传递$ table
<?php
include_once 'connect.php';
$table = '';
function showtabledata($table)
{
$result = mysql_query("SELECT * FROM ".$table);
if (!$result)
{
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<div class='sidemenu'>Table: {$table}</div>";
echo "<table><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell) {
echo "<td>$cell</td>";
}
echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>";
}
switch($table)
{
case '1':
$table = 'table1';
echo "table not available";
break;
case '2':
$table = 'table2';
showtabledata($table);
break;
case '3':
$table = 'table3';
showtabledata($table);
break;
case '4':
default:
$table = 'table4';
showtabledata($table);
}
?> ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.