Skip to content
Advertisement

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

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.

JavaScript

Advertisement

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.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement