我可以改善我的PDO方法吗(刚刚开始) [英] Can I improve my PDO method (just started)

查看:52
本文介绍了我可以改善我的PDO方法吗(刚刚开始)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从mySQLi(从mySQL)切换到PDO,到目前为止一切都很好,也很容易,尤其是关于准备好的语句

I just switched to PDO from mySQLi (from mySQL) and it's so far good and easy, especially regarding prepared statements

这是我准备好的带有准备好的陈述的东西

This is what I have for a select with prepared statement

主数据库文件(包含在所有页面中):

Main DB file (included in all pages):

class DBi {
    public static $conn;
    // this I need to make the connection "global"
}

try {

    DBi::$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbuname, $dbpass);
    DBi::$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    DBi::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}

catch(PDOException $e) {

    echo '<p class="error">Database error!</p>';

}

在我的页面中:

try {

    $sql = 'SELECT pagetitle, pagecontent FROM mypages WHERE pageid = ? LIMIT 1';

    $STH = DBi::$conn->prepare($sql);

    $STH->execute(array($thispageid));  // $thispageid is from a GET var

}

catch(PDOException $e) {
    echo '<p class="error">Database query error!</p>';
}

    if ($STH) {  // does this really need an if clause for it self?

        $row = $STH->fetch();

        if (!empty($row)) {  // was there found a row with content?

            echo '<h1>'.$row['pagetitle'].'</h1>
            <p>'.$row['pagecontent'].'</p>';

        }

    }

一切正常.但是我做对了吗?还是可以简化一些地方?

It all works. But am I doing it right? Or can I make it more simple some places?

是否使用if(!empty($ row)){}好的解决方案,以检查是否存在包含内容的结果行?在准备好的缩小选择中找不到其他体面的方法来检查数字

Is using if (!empty($row)) {} an ok solution to check if there was a result row with content? Can't find other decent way to check for numrows on a prepared narrowed select

推荐答案

catch(PDOException $e) {
    echo '<p class="error">Database query error!</p>';
}

我将借此机会记录哪个数据库查询错误.

I would use the opportunity to log which database query error occurred.

在此处查看示例: http://php.net/manual/en/pdostatement. errorinfo.php

如果您发现错误,也应该从函数或脚本中获取return.

Also if you catch an error, you should probably return from the function or the script.

if ($STH) {  // does this really need an if clause for it self?

如果$STH无效,则它应该已生成异常并已被先前捕获.而且,如果您已经从该catch块中的函数返回了,那么您就不会在代码中到此为止,因此无需再次测试$STH是否为非null.刚开始从中获取.

If $STH isn't valid, then it should have generated an exception and been caught previously. And if you had returned from the function in that catch block, then you wouldn't get to this point in the code, so there's no need to test $STH for being non-null again. Just start fetching from it.

    $row = $STH->fetch();

    if (!empty($row)) {  // was there found a row with content?

我会这样写:

$found_one = false;
while ($row = $STH->fetch()) {
    $found_one = true;
    . . . do other stuff with data . . .
}
if (!$found_one) { 
    echo "Sorry! Nothing found. Here's some default info:";
    . . . output default info here . . .
}

无需测试它是否为空,因为如果存在,则循环将退出.

No need to test if it's empty, because if it were, the loop would exit.

这篇关于我可以改善我的PDO方法吗(刚刚开始)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆