【问题标题】:More efficient way of inserting using PDO?使用 PDO 插入的更有效方法?
【发布时间】:2012-06-04 14:46:21
【问题描述】:

您好,每次将图像上传到我的服务器时,我都会将图像数据插入数据库。我使用的代码看起来有点“笨拙”,尤其是绑定。是否可以采取不同的方式来减少文本量并更快地执行,还是我不应该担心?

这是我正在使用的代码:

function($file_name, $cat, $year, $desc, $title, $image_size, $image_width, $image_height){
    //test the connection
    try {
        //connect to the database
        $dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw");
        //if there is an error catch it here
    } catch( PDOException $e ) {
        //display the error
        echo $e->getMessage();
    }

    $stmt = $dbh->prepare("INSERT INTO mjbox_images(img_file_name,img_cat,
        img_year,img_desc,img_title,img_size,img_width,img_height) 
        VALUES(?,?,?,?,?,?,?,?)");

        $stmt->bindParam(1,$file_name);
        $stmt->bindParam(2,$cat);
        $stmt->bindParam(3,$year);
        $stmt->bindParam(4,$desc);
        $stmt->bindParam(5,$title);
        $stmt->bindParam(6,$image_size);
        $stmt->bindParam(7,$image_width);
        $stmt->bindParam(8,$image_height);
        $stmt->execute();
    }

【问题讨论】:

  • 您可以简单地将所有值添加到数组中并执行它。尽管根据您的标题,这与效率无关。
  • 您可能还想查看如何disable the emulating of prepared statements。
  • 您可能应该在您的 catch 块中返回,因为您将在 PDO 的垃圾对象实例上运行查询(连接失败,因为 PDOException 会告诉您)。

标签: php mysql insert pdo bind


【解决方案1】:

根据您要重写多少代码,您总是可以从使用纯 PDO 切换到 RedBean 之类的东西(这实际上非常好,是一个零配置的 ORM)。

http://www.redbeanphp.com/

值得一看,即使您现在不会使用它;这绝对是一个很棒的工具。

然后插入将只需要修改 bean 属性,从而减少您使用的代码总量。

【讨论】:

    【解决方案2】:

    您可以这样做,传递一个值数组并使用键作为占位符,这样您就可以使用相同的函数插入不同的表:

    <?php 
    $insert = array('img_file_name'=>'',
                    'img_cat'=>'',
                    'img_year'=>'',
                    'img_desc'=>'',
                    'img_title'=>'',
                    'img_size'=>'',
                    'img_width'=>'',
                    'img_height'=>'');
    
    insert('mjbox_images',$insert);
    
    function insert($table,$values=array()){
    
        //test the connection
        try{
            //connect to the database
            $dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw");
            //if there is an error catch it here
        } catch( PDOException $e ) {
            //display the error
            echo $e->getMessage();
        }
    
        $fieldnames = array_keys($values);
    
        $sql = "INSERT INTO $table";
        $fields = '( ' . implode(' ,', $fieldnames) . ' )';
        $bound = '(:' . implode(', :', $fieldnames) . ' )';
        $sql .= $fields.' VALUES '.$bound;
    
        $stmt = $dbh->prepare($sql);
        $stmt->execute($values);// whoops
    }
    
    //INSERT INTO mjbox_images( img_file_name ,img_cat ,img_year ,img_desc ,img_title ,img_size ,img_width ,img_height ) VALUES (:img_file_name, :img_cat, :img_year, :img_desc, :img_title, :img_size, :img_width, :img_height )
    
    ?>
    

    【讨论】:

    • 每次用户想要插入内容时,您是否要打开一个新连接?那么更新、删除等呢?
    • 是否正在为我期望的 OP 编写完整的 CRUD?
    • 对于标题中有效的问题,我希望答案是有效的。这不是。在$stmt-&gt;execute() 调用中也没有插入任何内容。很抱歉,单身人士并没有更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2012-06-27
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    相关资源
    最近更新 更多