Code: Select all
<?php
//Get the variables from the form and connect to database
$db_connection = mysql_connect("$_POST[db_host]","$_POST[db_username]","$_POST[db_password]");
//Check if connection to the database was successful
if (!$db_connection)
{
die("<font color='red'><b>Error</b></font>: Could not connect to database: " . mysql_error());
}
echo "<font color='navy'><b>Connection Established</b>.<br></font>";
//Select database to connect to,
$db_database=mysql_select_db("$_POST[db_database]", $db_connection);
// Check if could connect to particular database,
if (!$db_database)
{
die("<font color='red'><b>Error</b></font>: Could not connect to the particular database: " . mysql_error());
}
echo "<font color='blue'><b>Success</b></font>: Connection established to database <b>$_POST[db_database]</b>.<br>";
//get the table name
$db_table_name=$_POST[db_table];
// Create table in my database
$db_table = "CREATE TABLE $db_table_name
(
db_host varchar(128),
db_username varchar(128),
db_password varchar(512)
)";
// Check if table exists,
if (!mysql_query($db_table,$db_connection))
{
die("<font color='red'><b>Error</b></font>: " . mysql_error());
}
echo "<font color='blue'><b>Success</b></font>: Table <b>$db_table_name</b> was created.<br>";
//Convert password to md5 string
$db_password = md5($_POST[db_password]);
echo "<font color='blue'><b>Success</b></font>: Converted password to <b>md5</b>.<br>";
//Insert the variable values
$db_insert="INSERT INTO $db_table_name (db_host, db_username, db_password)
VALUES
('$_POST[db_host]','$_POST[db_username]','$db_password')";
if (!mysql_query($db_insert,$db_connection))
{
die("<font color='red'><b>Error</b></font>: " . mysql_error());
}
echo "<font color='blue'><b>Success</b></font>: <b>Variables</b> were configured successfully.<br>";
//Close connection
mysql_close($db_connection);
echo "<font color='navy'><b>Connection Closed</b></font><br>";
?>and now I want to read all the rows and display all the data in an html table,
Code: Select all
<?php
//Get the variables from the form and connect to database
$db_connection = mysql_connect("$_POST[db_host]","$_POST[db_username]","$_POST[db_password]");
//Check if connection to the database was successful
if (!$db_connection)
{
die("<font color='red'><b>Error</b></font>: Could not connect to database: " . mysql_error());
}
echo "<font color='navy'><b>Connection Established</b>.<br></font>";
//Select database to connect to,
$db_database=mysql_select_db("$_POST[db_database]", $db_connection);
// Check if could connect to particular database,
if (!$db_database)
{
die("<font color='red'><b>Error</b></font>: Could not connect to the particular database: " . mysql_error());
}
echo "<font color='blue'><b>Success</b></font>: Connection established to database <b>$_POST[db_database]</b>.<br>";
//get the table name
$db_table_name=$_POST[db_table];
// Show tables from database
$db_table_read = "SELECT * FROM $db_table_name";
if (!mysql_query($db_table_read,$db_connection))
{
die("<font color='red'><b>Error</b></font>: " . mysql_error());
}
echo "<font color='blue'><b>Success</b></font>: Fetching database contents...<br><br>";
while ($db_table_contents = mysql_fetch_array($db_table_read)) {
echo $db_table_contents;
}
//Close connection
mysql_close($db_connection);
echo "<br><font color='navy'><b>Connection Closed</b></font><br>";
?>Any help on this?Connection Established.
Success: Connection established to database my_test.
Success: Fetching database contents...
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/myuser/public_html/tests/sql/show.php on line 38
Connection Closed