从网站设置与数据库的连接并运行查询

I am working on a project for a Database Systems class where we created a database and have to present it somehow. Our group had the idea to create an 'imdb' type website where various information about movies could be stored and presented to the user. We have our database created and are about to begin work on the front-end for the website.

We have some confusion among our group as to how to proceed. We understand that we need to use PHP to connect to the database with the username and password credentials, but from there:

  • Do you have to have the PHP connection code on every webpage, or just the home page?
  • What do the queries look like when communicating with the database to retrieve and display data?
  • Would a WordPress site be easier to use when working in this capacity as it's more or less a 'Proof of Concept' type project, not a fully fleshed out site?

Here are your answers:

  1. Yes- you will need to have the connection on every page. Usually, the connection parameters and script is located in one file which you require on every page.

  2. A query looks like:

    SELECT `name` FROM `movies` ORDER BY ID DESC LIMIT 0, 10;
    
  3. You may find some WordPress plugins which will do something similar. If it's a proof of concept, then I suggest you go this way (if such a plugin exists). It's also a good idea to read extensively on PHP/MySQL development or hire someone who can do that if you want to make that project real.

As itoctopus said, every page will need to connect to the database. I have found the easiest way to accomplish this is to build your db connection in a "header" file (a file that is "included" in every php that needs access to the db).