I made a php file that makes a table 'news' in the database. Then I stored in the cells of the table some values:
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'>Error</font>: Could not connect to database: " . mysql_error());
}
echo "<font color='blue'>Success</font>: Connection established.<br>";
//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'>Error</font>: Could not connect to the particular database: " . mysql_error());
}
echo "<font color='blue'>Success</font>: Connection established to database $_POST[db_database].<br>";
// Create table in my database
$db_table = "CREATE TABLE news
(
news_id varchar(1024),
news_title varchar(1024),
news_content varchar(1024),
news_tags varchar(1024)
)";
// Check if table exists,
if (!mysql_query($db_table,$db_connection))
{
die("<font color='red'>Error</font>: " . mysql_error());
}
echo "<font color='blue'>Success</font>: Table was created.<br>";
//Insert the variable values
$db_insert="INSERT INTO news (news_id, news_title, news_content, news_tags)
VALUES
('1','my title','my content','my tags')";
if (!mysql_query($db_insert,$db_connection))
{
die("<font color='red'>Error</font>: " . mysql_error());
}
echo "<font color='blue'>Success</font>: Default article was added successfully.";
//Various tests & Close connection
$db_insert="INSERT INTO news (news_id, news_title, news_content, news_tags)
VALUES
('2','my title2','my content3','my tags4')";
if (!mysql_query($db_insert,$db_connection))
{
die("<font color='red'>Error</font>: " . mysql_error());
}
echo "<font color='blue'>Success</font>: Second article was added successfully.";
//select news and display them..... but how do I display them in a table form????
$db_select = 'SELECT * FROM `news` ORDER BY `news`.`news_id` ASC LIMIT 0, 30 ';
if (!mysql_query($db_select,$db_connection))
{
die("<font color='red'>Error</font>: " . mysql_error());
}
echo "<font color='blue'>Success</font>: I am going to list your entries now: <br/>$db_select<br/>";
mysql_close($db_connection);
?>How do I return those entries in the form of an HTML table?
In phpMyAdmin the table shows correctly.
And also, can someone explain me the difference between varchar, int etc.?
And what's the best to use? I use varchar all the time.