insert query mysql throws PDO::exec() expects exactly 1 parameter, 2 given

Tags: , , ,



I’ve pieced together some code I’ve gleaned from the internet: I’m trying to scan a directory to insert file names and index into MariaDB table. My last hurdle it seems is this PDO error: PDO::exec() expects exactly 1 parameter, 2 given on line 55. I’ve tagged line(55) with ‘//error thrown here’. My novice guess is it doesn’t like the parameters escaped in []??

As noted above novice here… Any insight/help is greatly appreciated. Thanks in advance.

<?php

    $host = 'localhost';
    $dbname = 'dirdb';
    $username = 'root';
    $password = '';

    // Create connection
try {
  $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}   

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$dir = './recipes';

$GLOBALS['I'] = 0; // root folder given index 0

function dirToArray( $dir , $parent) {
    $result = array();
    $cdir = scandir($dir);
    foreach ($cdir as $key => $value) {
        if (!in_array($value, array(".", "..")))  {
            if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){

                $result[$value] = [++$GLOBALS['I']]; // add folder index
                $result[$value][] = $parent; // add parent folder index

                $result[$value][] = dirToArray($dir . DIRECTORY_SEPARATOR . $value, $GLOBALS['I']);
            } else {
                $result[] = $value;
            }
        }
    }
    return $result;
}

$res = dirToArray($dir, $GLOBALS['I']);


function dirToDb($res, $parentId = 0)
{global $conn;
    foreach ($res as $key => $value) {
        if (is_array($value))  {
            $conn->exec ("insert into sp_files (path, parentId) VALUES (?, ?)", [$key, $parentId]);  //error thrown here
            dirToDb($value, $conn->fetch("SELECT LAST_INSERT_ID()"));
        } else {
            $conn->exec ("insert into sp_files (path, parentId) VALUES (?, ?)", [$value, $parentId]);
        }
    }
}

//$res = dirToArray($dir);

dirToDb($res);

Answer

You can’t use $conn->exec() to execute a query with parameters. You have to use prepare() to create a statement, then execute the prepared statement.

There’s also no $conn->fetch() method. fetch() is a method of the PDOStatement class, you can use it either with a prepared statement or the result of a query. But you don’t need to perform a query to get LAST_INSERT_ID(), PDO has an insertId() method for this.

function dirToDb($res, $parentId = 0) {
    global $conn;
    $stmt = $conn->prepare("insert into sp_files (path, parentId) VALUES (?, ?)");
    foreach ($res as $key => $value) {
        $stmt->execute([$key, $parentId]); 
        if (is_array($value))  {
            dirToDb($value, $stmt->insertId);
        }
    }
}


Source: stackoverflow