doucuo1642 2013-12-24 15:08
浏览 47
已采纳

在PHP中为PDO查询创建容器函数

Because I find PDO executions extremely hard to remember and find myself looking back at previous projects or other websites just to remember how to select rows from a database, I decided that I would try and create my own functions that contain the PDO executions and just plug in the data I need. It seemed a lot simpler than it actually is though...

So far I have already created a connect function successfully, but now when it comes to create a select function I'm stumped for multiple reasons.
For starters there could be a variating amount of args that can be passed into the function and secondly I can't figure out what I should pass to the function and in which order.

So far the function looks like this. To keep me sane, I've added the "id" part to it so I can see what exactly I need to accomplish in the final outcome, and will be replaced by variables accordingly when I work out how to do it.

function sql_select($conn, **what to put here**) {
    try {
        $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
        $stmt->execute(array('id' => $id));

        $result = $stmt->fetchAll();

        if ( count($result) ) {
            foreach($result as $row) {
                print_r($row);
            }
        } else {
            return "No rows returned.";
        }
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}

So far what I've established that the function will need to do is

  • Connect to the database (using another function to generate the $conn variable, already done)
  • Select the table
  • Specify the column
  • Supply the input to match
  • Allow for possible args such as ORDER by 'id' DESC

Lastly from this I would need to create a function to insert, update and delete rows from the database.

Or, is there a better way to do this rather than functions?

If anyone could help me accomplish my ambitions to simply simplify PDO executions it would be greatly appreciated. Thanks in advance!

  • 写回答

4条回答 默认 最新

  • drvvvuyia15070493 2013-12-24 17:14
    关注

    First of all, I have no idea where did you get 10 lines

    $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = ?');
    $stmt->execute(array($id));
    $result = $stmt->fetchAll();
    

    is ALL the code you need, and it's actually three lines, which results with a regular PHP array that you can use wherever you wish. Without the need of any PDO code. Without the need of old mysql code.

    Lastly from this I would need to create a function to insert, update and delete rows from the database.

    DON'T ever do it.

    Please read my explanations here and here based on perfect examples of what you'll end up if continue this way.

    accomplish my ambitions to simply simplify PDO executions

    That's indeed a great ambition. However, only few succeeded in a real real simplification, but most resulted with actually more complex code. For starter you can try code from the first linked answer. Having a class consists of several such functions will indeed improve your experience with PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)