简体   繁体   English

PHP PDO:我的PHP可以更清洁/更高效吗?

[英]PHP PDO: Could my PHP be cleaner/more efficient?

So I've finally decided to update my PHP for the year 2012 by learning to use PHP's PDO. 因此,我最终决定通过学习使用PHP的PDO来更新2012年的PHP。 So far everything is going great, however I don't know if the way I'm going about it is really the best way of doing it. 到目前为止,一切都很顺利,但是我不知道我的想法是否真的是最好的方法。

In this example I am querying my database to display posts users have made, and then displaying 2 comments for each post. 在此示例中,我要查询数据库以显示用户发表的帖子,然后为每个帖子显示2条评论。 So basically what I'm doing is grabbing my posts, looping it out, then in that loop I query the database for the top two comments for every post. 因此,基本上我正在做的是抓住我的帖子,将其循环播放,然后在该循​​环中,我在数据库中查询每个帖子的前两个注释。 However before I run around and start using this all year, I figured I'd see if there is a cleaner method of doing this. 但是,在我全年跑步并开始使用此功能之前,我想知道是否有一种更清洁的方法可以这样做。

So if anyone could spare a moment to look over this small block of code and let me know if there is a cleaner and perhaps more efficient way of doing this I'd really appreciate it. 因此,如果任何人都可以花一点时间看一下这小段代码,并让我知道是否有一种更清洁,也许更有效的方式来做到这一点,我将非常感激。 Feel free to nitpick! 随意挑剔!

<?php
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'database';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Get Posts
    $stmt = $dbh->prepare("SELECT * FROM posts");
    $stmt->execute();
    $result = $stmt->fetchAll();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

//Loop through each post
foreach($result as $row) {

echo $row['post'];

//Get comments for this post
    $pid = $row['id'];
    $stmt = $dbh->prepare("SELECT * FROM comments WHERE pid = :pid LIMIT 2");
    $stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
    $stmt->execute();
    $c_result = $stmt->fetchAll();

//Loop through comments
foreach($c_result as $com) {

echo $com['comment'];

    }
}
//Close connection
$dbh = null;
?>

Well, it is actually 2 questions. 好吧,实际上是两个问题。

.1. .1。 For the code you are using, it is quite ugly. 对于您使用的代码 ,这非常难看。 Using raw API functions always makes your code ugly, boring and repetitive. 使用原始API函数总是会使您的代码难看,枯燥且重复。 To run just one query took you FIVE lines! 只运行一个查询就花了五行!
Don't you think that just one line would be better? 您不认为仅一行就更好了吗? Line consists of only meaningful operators? 行仅由有意义的运算符组成?

$comments = $db->getAll("SELECT * FROM comments WHERE pid = :pid LIMIT 2",$row['id']);

.2. .2。 For the algorithm - it is quite okay. 对于算法-很好。
Assuming you are * not going to loop over all your database, but merely request only 10-20 posts per page, additional 10-20 primary-key based lookups won't slow your application much. 假设您* 不会遍历所有数据库,而仅每页仅请求10-20个帖子,那么额外的基于10-20个主键的查找不会使您的应用程序变慢。

.3. .3。 Bonus track. 奖金轨道。
thing you really may want to consider is a "business logic/presentation logic separation". 您可能真正要考虑的事情是“业务逻辑/表示逻辑分离”。 Why not to get all your data first and only than starting an output? 为什么不先获取所有数据而不是开始输出呢? it will make your code way more clean. 它将使您的代码更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM