Create links with a mysql table name and display its contents on another page. Is it possible?

Asked

Viewed 1,024 times

0

I’m trying to create a php page that fetches the name of several tables from a database, displays table names and creates a link to each table. Once the user clicks on the link, it is taken to another page where the contents of that table are shown. The problem is I’m not seeing a way to display table names and create links automatically.
Someone can help out?

2 answers

5

There are some ways to get the table names, such as

SHOW TABLES;

or

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'NOME_DO_SCHEMA';

With this you can build a link like /conteudoTabela.php?tabela=NOME_DA_TABELA.

  • +1: If using with multiple Databases, the second query is better. If using a database only, the second query can consume too many resources, use the first.

  • Also remembering the SHOW DATABASES

0

Tries:

<?php
$dbname = 'mysql_dbname';

if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
    echo 'Could not connect to mysql';
    exit;
}

$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);

while ($row = mysql_fetch_row($result)) {
    echo "Tabela: <a href='show_data.php?table=".$row[0]."'<br />";
}

?>

show_data.php

<?php
$db = 'nome_database';

$c = mysql_connect('host', 'user', 'pass');
$b = mysql_select_db($db, $c);

$tabela = $_GET['table'];

$sql = mysql_query("SELECT * FROM ".$tabela."");

while($data = mysql_fetch_array($sql)){

echo $data['campo1'];
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.