I am currently trying to create a PHP script whereby there is already a login script created and the user that successfully logs into the home.php is able to view the tables of the database he is logged into.
Is there any ways of retrieving the tables using the mySQL query like “SHOW TABLES”?
Here are some of my codes:
<?php
session_start();
if($_SESSION['id'])
{
echo "Welcome ",$_SESSION['username']."<br>";
echo "Click here to Logout : ".'<br><a href="logout.php">Logout</a>';
}
else
{
echo "You don't belong here!";
}
$tableSQL = "SHOW TABLES";
$tablesoutput = mysql_query($tableSQL);
print $tablesoutput;
?>
I think my codes are wrong as there are errors showing that access is denied to the database. Does it mean that I have to reconnect to the database even though I have a session established?
A session only means that values stored in the
$_SESSIONvariable are persisted across separate requests, but that’s all. It does not “log a user into the database” nor does it keep a database connection open. You’ll have to establish a database connection on every page load.You also need to use
mysql_fetch_assocon the result ofmysql_query. A simpleprintwon’t suffice.