3

I'm trying to insert a row in a MySQL database using PDO, and I'm using parameter binding, but I'm getting a stranger error. The important functions are:

private static function getConnection() : \Pdo
{
    $con = new \PDO('mysql:host=localhost;dbname=mydatabase;charset=utf8', 'myuser', 'mypassword');
    return $con;
}

public static function runBool(string $query, ?array $bind = []) : bool
{
    return self::getConnection()
        ->prepare($query)
        ->execute($bind);
}

As you can see, getConnection() is only returning a connection (and it's working fine), the problem is with the function runBool() which is throwing an error on the ->execute($bind); line:

Exception: Parameter 'index' must be a int

For example, my $query is

INSERT INTO client (company_id, name, phone, cpf, rg, city_id, address, obs) VALUES (:company_id, :name, :phone, :cpf, :rg, :city_id, :address, :obs)

And my $bind is

array(8) {
    ["company_id"]=> int(1)
    ["name"]=> string(3) "John Doe"
    ["phone"]=> string(11) "123456789"
    ["cpf"]=> string(0) ""
    ["rg"]=> string(0) ""
    ["city_id"]=> int(79)
    ["address"]=> string(0) ""
    ["obs"]=> string(0) ""
}

I've already tryed to foreach $bind and to use bindParam() or bindValue() individually, and then run execute but I get the same error on the execute(). I've already included : before each key of the array and removed it (following this comment), but nothing changes. I've already searched a lot on Google but nobody seems to have the same type of trouble.

Info:

  • PHP 7.1.12
  • Ubuntu 16.04.03
  • MySQL 5.7.20
  • Vagrant 2.0.0

Every help is appreciated.

---------------- EDIT ----------------

As asked, I've checked my namespaces, but I'm using \Pdo, so I think it's the native PHP PDO class. Here's the var_dump of self::getConnection()->prepare($query):

object(PDOStatement)#90 (1) { ["queryString"]=> string(149) "INSERT INTO client (company_id, name, phone, cpf, rg, city_id, address, obs) VALUES (:company_id, :name, :phone, :cpf, :rg, :city_id, :address, :obs)" }

And here's my table:

CREATE TABLE `client` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(50) NOT NULL,
  `phone` varchar(15) NOT NULL,
  `city_id` int(10) UNSIGNED NOT NULL,
  `address` varchar(100) DEFAULT NULL,
  `cpf` varchar(14) DEFAULT NULL,
  `rg` varchar(15) DEFAULT NULL,
  `obs` varchar(100) DEFAULT NULL,
  `company_id` int(10) UNSIGNED NOT NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `client`
  ADD PRIMARY KEY (`id`),
  ADD KEY `city_id` (`city_id`),
  ADD KEY `company_id` (`company_id`);
ALTER TABLE `client` ADD FULLTEXT KEY `search` (`name`,`phone`,`address`,`cpf`,`rg`,`obs`);
ALTER TABLE `client` ADD FULLTEXT KEY `search_basic` (`name`);

ALTER TABLE `client`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=613;

ALTER TABLE `client`
  ADD CONSTRAINT `client-city_id` FOREIGN KEY (`city_id`) REFERENCES `city` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  ADD CONSTRAINT `client-company_id` FOREIGN KEY (`company_id`) REFERENCES     `company` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
ayrtonvwf
  • 304
  • 1
  • 8

2 Answers2

1

Change your $bind variable to

array(8) {
    [":company_id"]=> int(1)
    [":name"]=> string(3) "John Doe"
    [":phone"]=> string(11) "123456789"
    [":cpf"]=> string(0) ""
    [":rg"]=> string(0) ""
    [":city_id"]=> int(79)
    [":address"]=> string(0) ""
    [":obs"]=> string(0) ""
}
Kingson
  • 149
  • 5
1

First check what PDO are u using, check namespaces, because error that u get it's not a PDO error, it is a Phalcon result set error, here is a git: https://github.com/phalcon/cphalcon/blob/master/ext/phalcon/mvc/model/resultset.zep.c#L366

Phalcon try build a result_set.

Can you provide var_dump of self::getConnection()->prepare($query); before executing.

And provide DDL of your table.

Daredzik
  • 422
  • 2
  • 9
  • 21
  • Wow, that'll help me a lot. I'll just be able to test it in about 10 hours, but seems the right way to be looking at. I thought I was using the pure PHP PDO, but I can mistakely be using some Phalcon class. – ayrtonvwf Dec 08 '17 at 10:26
  • I've just edited my question with the var_dump, it seems that I'm using the native PHP PDO class, but it's stranger cause I'm do using the Phalcon Framework, so it can be related. – ayrtonvwf Dec 08 '17 at 20:37