How to use PHP and CGI to develop news websites
With the rapid development of the Internet, news websites have become an important way for people to obtain news information. Developing an efficient, stable, and easy-to-use news website is an important task for website developers. This article will introduce how to use PHP and CGI to develop a news website, and provide some code examples to help readers better understand.
First, we need to build a web server to run our news website. Here we can use Apache server. For specific steps to set up PHP and CGI environments on the server, please refer to the relevant documentation of the server.
Next, we need to create a database to store news information. Here we can use MySQL database. First, we need to create a news table containing fields such as title, content, release time, etc.
CREATE TABLE news ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, content TEXT, publish_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Then, we can use PHP to connect to the database and implement related functions. The following is a sample code for adding news:
prepare("INSERT INTO news (title, content) VALUES (:title, :content)"); $stmt->bindParam(':title', $title); $stmt->bindParam(':content', $content); $stmt->execute(); // 重定向到新闻列表页面 header("Location: news_list.php"); exit; } ?>
Through the above code, we can get the news titles and content submitted by the user and insert them into the database. After that, we use the header function to redirect the user to the news list page.
Next, we need to write a CGI script to handle the user's request to access the news list page, and obtain the news list from the database and display it on the page. The following is a sample code for a news list page:
#!/usr/bin/php-cgi query('SELECT * FROM news ORDER BY publish_time DESC'); $newsList = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>新闻列表 新闻列表
Through the above code, we can get the news list from the database and display it on the page. Each news title is a link that can be clicked to jump to the news details page.
In addition to the news list page, we also need to write a CGI script to handle the user's request to access the news details page, and obtain the news content from the database and display it. The following is a sample code for a news details page:
#!/usr/bin/php-cgi prepare("SELECT * FROM news WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $news = $stmt->fetch(PDO::FETCH_ASSOC); ?>
Through the above code, we can obtain the news content from the database based on the news ID requested by the user and display it on the page.
To sum up, it is not complicated to develop a news website using PHP and CGI. We can implement related functions by building a web server, creating a database, and writing PHP and CGI scripts. I hope this article can provide some help to readers in developing news websites.
The above is the detailed content of How to develop a news website using PHP and CGI. For more information, please follow other related articles on the PHP Chinese website!