We can use the news system to store news, we can add to the news, delete, and so on, so as to reduce the workload of everyone, why not experiment.
First, create a table.
CREATE TABLE News (
n_id Int (255) NOT NULL auto_increment,
N_date datetime NOT NULL,
News text is not NULL,
PRimary Key (n_id)
);
The second step, set your login information
$database _user_name= "root";
$database _password= "";
$database _name= "News";
$time _offset= "0";
The third step, let's use the things behind the program as a function, save space Ah!
function connect_db ()
{
Connects to the database
Global $database _user_name, $database _password;
$db =mysql_connect ("localhost", $database _user_name, $database _password);
return $db;
}
function db_name ()
{
Returns the name of the database
Global $database _name;
$db _name= $database _name;
return $db _name;
}
function Get_now ()
{
Gets current date and time
$db =connect_db ();
$db _name=db_name ();
mysql_select_db ($db _name, $db);
$sql = "Select Now () as now";
$result =mysql_query ($sql, $db);
$myrow =mysql_fetch_array ($result);
$now = $myrow ["Now"];
return $now;
}
Fourth step, let's consider how to show the news
Library of functions already defined above ...
The definition of a table ...
$title ";
if ($admin ==1)
{
echo "Admin";
}
echo "-News
";
$db =connect_db ();
$db _name=db_name ();
mysql_select_db ($db _name, $db);
if ($show _news_sub)
{
$show _news = $show _news_sub;
}
if (!ereg ("([0-9]{]. strlen ($show _news)."}) ", $show _news))
{
$show _news= "All";
}
if (Strtoupper ($show _news) = = "All")
{
$sql = "Select Date_format (n_date, '%m/%d/%y ') as N_date, Date_format (n_date, '%h:%i ') as N_time, news, n_date as date from n EWS ORDER BY date Desc ";
}
Else
{
$link = "All News";
$sql = "Select Date_format (n_date, '%m/%d/%y ') as N_date, Date_format (n_date, '%h:%i ') as N_time, news, n_date as date from News ORDER BY n_date desc limit $show _news ";
}
$result =mysql_query ($sql, $db);
$num _rows=mysql_num_rows ($result);
if ($num _rows!=0)
{
echo "
";
while ($myrow =mysql_fetch_array ($result))
{
$date = $myrow ["N_date"];
$time = $myrow ["N_time"];
$news = $myrow ["News"];
$n _id= $myrow ["n_id"];
if (Strtoupper ($show _date) = = "Y" && strtoupper ($show _time) = = "Y")
{
$show = $date. " ". $time;
}
else if (Strtoupper ($show _date) = = "Y" && strtoupper ($show _time)! = "Y")
{
$show = $date;
}
if (Strtoupper ($show _date)! = "Y" && strtoupper ($show _time) = = "Y")
{
$show = $time;
}
$cleannews = (NL2BR ($news));
$show
$cleannews
";
}
echo "
";
}
if ($admin ==1)
{
echo "
Return
";
}
if ($link)
{
echo "
$link ";
}
Description: Omitted the format of the label, so it looks clear, you have to add AH
The above describes the use of PHP to create a news system, including the content of the news system, I hope that the PHP tutorial interested in a friend helpful.