PHP / MySQL:基于创建大型网站数据库的正确方法

时间:2022-10-13 11:57:56

I'm creating a movies website, IMDB.com like.. I'm really new to PHP and programming at all but I have some books and * of course :)

我正在创建一个电影网站,IMDB.com就像..我真的很新的PHP和编程,但我有一些书和*当然:)

I have already done lot of the work, but now I have more than 600 lines of code (PHO only) per page and more than 20 database tables only for storing and geting the movie data (many-to-many relationships)

我已经做了很多工作,但现在我每页有超过600行代码(仅限PHO)和超过20个数据库表,仅用于存储和确定电影数据(多对多关系)

Most of the code are MySQLi queries (prepared statements) to insert/get data and loops. Nothing fancy, just basic PHP/MySQL.

大多数代码都是MySQLi查询(预处理语句)来插入/获取数据和循环。没什么好看的,只是基本的PHP / MySQL。

I have some questions:

我有一些问题:

  • It is recommended to use MySQLi prepared statements for every SQL query or is better just to use old MySQL for internal PHP/database queries and leave prepared statements for user input only?

    建议为每个SQL查询使用MySQLi预处理语句,或者更好的方法是使用旧MySQL进行内部PHP /数据库查询,并仅为用户输入保留预准备语句?

  • To get all the movie data and show it I need to get the data from more than 16 different tables. I use one MySQL query per table (somethimes more) and lots of loops. This is the correct way a website need to work?? I mean.. this is normal???

    要获取所有电影数据并显示它,我需要从超过16个不同的表中获取数据。我每个表使用一个MySQL查询(一些更多)和许多循环。这是网站需要工作的正确方法吗?我的意思是..这是正常的???

  • How can I simplify the code to simplify the reading? Can I separete all those queries to external files?? Can I create functions to handle each table query??

    如何简化代码以简化阅读?我可以将所有这些查询分配给外部文件吗?我可以创建函数来处理每个表查询吗?

Hope you can give me a hand and maybe you have some more suggestions for correctly building such a website.

希望你能帮我一臂之力,也许你还有一些关于正确建立这样一个网站的建议。

Thanks!!

5 个解决方案

#1


Consider looking at or using a web framework for your website.

考虑为您的网站查看或使用Web框架。

symfony CakePHP CodeIgniter

symfony CakePHP CodeIgniter

Are some of the more mainstream ones. Learn from them if anything.

是一些更主流的。如果有的话,向他们学习。

#2


As mentioned in the anwsers above I would also point you to using a framework that impliments the MVC design pattern. Along with that most of the frameworks have an ORM built in but if they do not you can look at Symphony or EZPDO is another good ORM to tie into your Model in (M)VC.

正如上面的anwsers中所提到的,我还会指出你使用一个可以实现MVC设计模式的框架。除此之外,大多数框架都内置了ORM,但如果不是,您可以查看Symphony或EZPDO是另一个很好的ORM,可以与(M)VC中的模型绑定。

CodeIgniter is a very fast and light weight MVC framework that would allow you to bootstrap pretty quickly but you may also want to look into ZF (ZendFramework). ZF has a great framework feature set and is pretty flexible overall.

CodeIgniter是一个非常快速且轻量级的MVC框架,它允许您快速引导,但您可能还想查看ZF(ZendFramework)。 ZF有一个很棒的框架功能集,整体上非常灵活。

Beyond that be sure to seperate your reads and your writes in either or Model or your calls to the ORM of choice. This will allow you to slave data to multiple MySQL Boxes for bigger performance but allows you to use one database engine to start out.

除此之外,请确保将您的读取和写入分别用于或者模型或您对所选ORM的调用。这将允许您将数据从属数据传输到多个MySQL Box以获得更高的性能,但允许您使用一个数据库引擎启动。

Add in the ability to use Memcached so that you can cache your data/objects vs hitting the database.

添加使用Memcached的功能,以便您可以缓存数据/对象与访问数据库。

When using cache put some thought into how you would expire cache for a database update. In other words if your selecting data from a database to display in a view and that data is has not changed you should not need to hit the database every time but rather pull it from memory. Once the data actually does change you'd want to invalidate that cache so it's not stale and then re-cache the new data.

使用缓存时,请考虑如何使数据库更新的缓存失效。换句话说,如果您从数据库中选择数据以在视图中显示并且数据未更改,则您不应每次都访问数据库,而是从内存中提取数据。一旦数据确实发生了变化,您就会想要使该缓存无效,因此它不会过时,然后重新缓存新数据。

memcached: http://www.danga.com/memcached/'

-facebook also has a version

-facebook也有一个版本

CodeIgniter - http://codeigniter.com/

CodeIgniter - http://codeigniter.com/

EZPDO - http://www.ezpdo.net/blog/?p=2

EZPDO - http://www.ezpdo.net/blog/?p=2

ZendFramework -http://framework.zend.com/

#3


  1. Prepared statements are just fine for your own internal queries to. You'll have a strutured approach for all queries.

    准备好的语句适合您自己的内部查询。对于所有查询,您都将采用结构化方法。

  2. Well it depends on what you're showing. But I would say that you normally could use joins to get the data you need from more tables. You'll have a lot less quering to get the correct data, and it sounds like all your data is connected somehow to the one movie you're showing.

    那取决于你所展示的内容。但我会说你通常可以使用连接从更多的表中获取所需的数据。你可以获得更少的数据来获得正确的数据,听起来你的所有数据都以某种方式连接到你正在展示的那部电影。

  3. As Peter D comments, I would recommend using a web framework to learn how to seperate out the database handling from the view. Are you using an object oriented approach now? Look at the MVC pattern that some of these frameworks implement, that'll get you going.

    正如Peter D评论的那样,我建议使用Web框架来学习如何从视图中分离出数据库处理。你现在使用面向对象的方法吗?看看其中一些框架实现的MVC模式,这将让你前进。

#4


Like Peter D mention before add this one to the list of framework to use. Zend Framework http://framework.zend.com Open source and free.

就像Peter D之前提到的那样,将这一个添加到要使用的框架列表中。 Zend Framework http://framework.zend.com开源和免费。

  1. It is recommended to use MySQLi ...
  2. 建议使用MySQLi ...

Definitely MySQLi, but it's a big question by itself, if you start coding you'll need to grasp the basic of T-SQL to understand the difference.

绝对是MySQLi,但这本身就是一个很大的问题,如果你开始编码,你需要掌握T-SQL的基础来理解差异。

  1. To get all the movie data ...
  2. 获取所有电影数据......

It depends on alot of things. Size of database. Wanted results i.e. the information that need to be displayed, response time of the queries vs displaying in user view. Do you know about JOIN, UNION?

这取决于很多事情。数据库的大小。想要的结果,即需要显示的信息,查询的响应时间与在用户视图中显示的响应时间。你知道JOIN,UNION吗?

  1. How can I simplify the code to ...
  2. 如何将代码简化为......

Yes to all theses questions. www.w3schools.com/php/ if it can be of any help and learn the MVC pattern. Useful to alot of programming language these days. Maybe a framework would help you here

对所有这些问题都是肯定的。 www.w3schools.com/php/如果它可以提供任何帮助并学习MVC模式。这些天对很多编程语言很有用。也许一个框架可以帮助你

#5


To this questions:

对于这个问题:

"To get all the movie data and show it I need to get the data from more than 16 different tables. I use one MySQL query per table (somethimes more) and lots of loops. This is the correct way a website need to work?? I mean.. this is normal???"

“要获取所有电影数据并显示它我需要从超过16个不同的表中获取数据。我每个表使用一个MySQL查询(更多时间)和大量循环。这是网站需要工作的正确方法吗?我的意思是......这是正常的???“

No. If I understand you correctly, you should be using some type of JOIN depending on the data you're retrieving from the database. Getting results for huge amounts of data, and then picking out only the pieces you want in PHP is much slower than letting the database do the work of sorting/retrieving only the records/info you want to show.

不。如果我理解正确,您应该使用某种类型的JOIN,具体取决于您从数据库中检索的数据。获取大量数据的结果,然后只选择你想要的PHP,比让数据库完成排序/检索你想要显示的记录/信息的工作要慢得多。

I highly recommend a somewhat dated but very easy to grasp book that covers PHP and MySQL/Databases in general: http://www.dmcinsights.com/phpmysql2/ - It covers a lot of "in practice" techniques along with the code, so it'd be great to learn from.

我强烈推荐一本有点过时但很容易掌握的书,一般涵盖PHP和MySQL /数据库:http://www.dmcinsights.com/phpmysql2/ - 它涵盖了很多“实践中”技术以及代码,从中吸取教训会很棒。

There is a apparently a third edition with updated info, but I have not looked at it.

显然有第三版有更新的信息,但我没有看过它。

#1


Consider looking at or using a web framework for your website.

考虑为您的网站查看或使用Web框架。

symfony CakePHP CodeIgniter

symfony CakePHP CodeIgniter

Are some of the more mainstream ones. Learn from them if anything.

是一些更主流的。如果有的话,向他们学习。

#2


As mentioned in the anwsers above I would also point you to using a framework that impliments the MVC design pattern. Along with that most of the frameworks have an ORM built in but if they do not you can look at Symphony or EZPDO is another good ORM to tie into your Model in (M)VC.

正如上面的anwsers中所提到的,我还会指出你使用一个可以实现MVC设计模式的框架。除此之外,大多数框架都内置了ORM,但如果不是,您可以查看Symphony或EZPDO是另一个很好的ORM,可以与(M)VC中的模型绑定。

CodeIgniter is a very fast and light weight MVC framework that would allow you to bootstrap pretty quickly but you may also want to look into ZF (ZendFramework). ZF has a great framework feature set and is pretty flexible overall.

CodeIgniter是一个非常快速且轻量级的MVC框架,它允许您快速引导,但您可能还想查看ZF(ZendFramework)。 ZF有一个很棒的框架功能集,整体上非常灵活。

Beyond that be sure to seperate your reads and your writes in either or Model or your calls to the ORM of choice. This will allow you to slave data to multiple MySQL Boxes for bigger performance but allows you to use one database engine to start out.

除此之外,请确保将您的读取和写入分别用于或者模型或您对所选ORM的调用。这将允许您将数据从属数据传输到多个MySQL Box以获得更高的性能,但允许您使用一个数据库引擎启动。

Add in the ability to use Memcached so that you can cache your data/objects vs hitting the database.

添加使用Memcached的功能,以便您可以缓存数据/对象与访问数据库。

When using cache put some thought into how you would expire cache for a database update. In other words if your selecting data from a database to display in a view and that data is has not changed you should not need to hit the database every time but rather pull it from memory. Once the data actually does change you'd want to invalidate that cache so it's not stale and then re-cache the new data.

使用缓存时,请考虑如何使数据库更新的缓存失效。换句话说,如果您从数据库中选择数据以在视图中显示并且数据未更改,则您不应每次都访问数据库,而是从内存中提取数据。一旦数据确实发生了变化,您就会想要使该缓存无效,因此它不会过时,然后重新缓存新数据。

memcached: http://www.danga.com/memcached/'

-facebook also has a version

-facebook也有一个版本

CodeIgniter - http://codeigniter.com/

CodeIgniter - http://codeigniter.com/

EZPDO - http://www.ezpdo.net/blog/?p=2

EZPDO - http://www.ezpdo.net/blog/?p=2

ZendFramework -http://framework.zend.com/

#3


  1. Prepared statements are just fine for your own internal queries to. You'll have a strutured approach for all queries.

    准备好的语句适合您自己的内部查询。对于所有查询,您都将采用结构化方法。

  2. Well it depends on what you're showing. But I would say that you normally could use joins to get the data you need from more tables. You'll have a lot less quering to get the correct data, and it sounds like all your data is connected somehow to the one movie you're showing.

    那取决于你所展示的内容。但我会说你通常可以使用连接从更多的表中获取所需的数据。你可以获得更少的数据来获得正确的数据,听起来你的所有数据都以某种方式连接到你正在展示的那部电影。

  3. As Peter D comments, I would recommend using a web framework to learn how to seperate out the database handling from the view. Are you using an object oriented approach now? Look at the MVC pattern that some of these frameworks implement, that'll get you going.

    正如Peter D评论的那样,我建议使用Web框架来学习如何从视图中分离出数据库处理。你现在使用面向对象的方法吗?看看其中一些框架实现的MVC模式,这将让你前进。

#4


Like Peter D mention before add this one to the list of framework to use. Zend Framework http://framework.zend.com Open source and free.

就像Peter D之前提到的那样,将这一个添加到要使用的框架列表中。 Zend Framework http://framework.zend.com开源和免费。

  1. It is recommended to use MySQLi ...
  2. 建议使用MySQLi ...

Definitely MySQLi, but it's a big question by itself, if you start coding you'll need to grasp the basic of T-SQL to understand the difference.

绝对是MySQLi,但这本身就是一个很大的问题,如果你开始编码,你需要掌握T-SQL的基础来理解差异。

  1. To get all the movie data ...
  2. 获取所有电影数据......

It depends on alot of things. Size of database. Wanted results i.e. the information that need to be displayed, response time of the queries vs displaying in user view. Do you know about JOIN, UNION?

这取决于很多事情。数据库的大小。想要的结果,即需要显示的信息,查询的响应时间与在用户视图中显示的响应时间。你知道JOIN,UNION吗?

  1. How can I simplify the code to ...
  2. 如何将代码简化为......

Yes to all theses questions. www.w3schools.com/php/ if it can be of any help and learn the MVC pattern. Useful to alot of programming language these days. Maybe a framework would help you here

对所有这些问题都是肯定的。 www.w3schools.com/php/如果它可以提供任何帮助并学习MVC模式。这些天对很多编程语言很有用。也许一个框架可以帮助你

#5


To this questions:

对于这个问题:

"To get all the movie data and show it I need to get the data from more than 16 different tables. I use one MySQL query per table (somethimes more) and lots of loops. This is the correct way a website need to work?? I mean.. this is normal???"

“要获取所有电影数据并显示它我需要从超过16个不同的表中获取数据。我每个表使用一个MySQL查询(更多时间)和大量循环。这是网站需要工作的正确方法吗?我的意思是......这是正常的???“

No. If I understand you correctly, you should be using some type of JOIN depending on the data you're retrieving from the database. Getting results for huge amounts of data, and then picking out only the pieces you want in PHP is much slower than letting the database do the work of sorting/retrieving only the records/info you want to show.

不。如果我理解正确,您应该使用某种类型的JOIN,具体取决于您从数据库中检索的数据。获取大量数据的结果,然后只选择你想要的PHP,比让数据库完成排序/检索你想要显示的记录/信息的工作要慢得多。

I highly recommend a somewhat dated but very easy to grasp book that covers PHP and MySQL/Databases in general: http://www.dmcinsights.com/phpmysql2/ - It covers a lot of "in practice" techniques along with the code, so it'd be great to learn from.

我强烈推荐一本有点过时但很容易掌握的书,一般涵盖PHP和MySQL /数据库:http://www.dmcinsights.com/phpmysql2/ - 它涵盖了很多“实践中”技术以及代码,从中吸取教训会很棒。

There is a apparently a third edition with updated info, but I have not looked at it.

显然有第三版有更新的信息,但我没有看过它。