Preparing SQL Statements with PDO

后端 未结 4 1104
面向向阳花
面向向阳花 2021-01-15 12:59

My code looks like this:

// Connect to SQLite DB
DB(\'/path/to/sqlite.db\');

DB(\'BEGIN TRANSACTION;\');

// These loops are just examples.
for ($i = 1; $i          


        
4条回答
  •  甜味超标
    2021-01-15 13:05

    Unfortunately I think that the problem may be with the structure of your code.

    In your loop of INSERT statements, the statements are all identical and there is no need to call $db->prepare each time. The idea behind prepared statements is that you call $db->prepare() once, and execute() can be called multiple times on the same statement object. You're calling $db->prepare() every time, which is causing overhead in parsing the SQL statement and creating a new object.

    Consider re-writing your DB() function like this:

    function do_query($db, $pdo_statement, $query, $args)
    {
        if ($pdo_statement->execute($args) === true)
        {
            if (stripos($query, 'INSERT') === 0)
            {
              return $db->lastInsertId();
            }
            if (stripos($query, 'SELECT') === 0)
            {
              return $result->fetchAll(PDO::FETCH_ASSOC);
            }
            return $result->rowCount();
        }
    }
    
    function DB($query)
    {
        static $db = null;
    
        if (is_file($query) === true)
        {
          $db = new PDO('sqlite:' . $query, null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
        }
    
        else if (is_a($db, 'PDO') === true)
        {
          $result = $db->prepare($query);
    
          if (is_a($result, 'PDOStatement') === true)
          {
            $args = func_get_args();
            if (is_array($args[1])) {
                $ret = array();
                foreach ($args[1] as $args) {
                    $ret[] = do_query($db, $query, $result, $args);
                }
                return $ret;
            }
    
            return do_query($db, $query, $result, array_slice(func_get_args(), 1));
          }
    
          return false;
        }
    
        return true;
    }
    

    So if you want to run the same query with lots of values, you create two-dimensional array of the values you want to insert, and call DB('INSERT INTO....', $values). The DB() function checks to see if the second parameter to the function (after $query) is an array and if so it loops through running $query against the values in the array. This way, the loop doesn't involve re-preparing the SQL statement each time, just re-executing it with different values. The return value of the function will be an array of the results of each query.

提交回复
热议问题