0

How do I insert an array into crate?

I'm using the latest Crate-PDO, but keep receiving this notice

Notice: Array to string conversion in .../Crate/PDO/PDOStatement.php on line 610

and

Array to string conversion in .../myPdoClass.php on line 118

I'm using this code:

$myArray = ['1', '2', '3', '4', '5'];

$db = new Database;
$db->Query("insert into tbl (id, arrayCol) values (?,?)");
$db->bind(1, '1');
$db->bind(2, $myArray); 
$db->execute();

Any help would be great, thanks

4 Answers4

2

To bind any non-string parameter you must use the PDO bindValue() method passing in the wanted type as the 3rd parameter. Example:

$statement->bindValue(2, [1, 2], PDO::PARAM_ARRAY)
Sebastian Utz
  • 719
  • 3
  • 9
  • I've tried this, however I then get " Fatal error: Undefined class constant 'PARAM_ARRAY' in" – Blowfish Tech Aug 05 '15 at 11:29
  • 1
    either import the crate pdo namespace, like: `use Crate\PDO\PDO` or use the FQ classname: Crate\PDO\PDO::PARAM_ARRAY – Sebastian Utz Aug 05 '15 at 11:30
  • I've adding this, require ("../../../vendor/crate/crate-do/src/Crate/PDO/PDO.php");use Crate\PDO\PDO; To myPdoClass.php and i still get 'Fatal error: Undefined class constant 'PARAM_ARRAY' in' – Blowfish Tech Aug 05 '15 at 12:40
  • have you tried using the FQ classname? btw. this is the test at crate-pdo validating the array type support: https://github.com/crate/crate-pdo/blob/master/test/CrateIntegrationTest/PDO/PDOStatementTest.php#L253 – Sebastian Utz Aug 06 '15 at 11:21
  • this is the correct solution," either import the crate pdo namespace, like: use Crate\PDO\PDO or use the FQ classname: Crate\PDO\PDO::PARAM_ARRAY " from @sebastian many thanks – Blowfish Tech Aug 07 '15 at 12:30
-1

EDIT : This a quick fix for the given error, not a way to store arrays.

You have to explicitly convert your array to a string. A simple way to do this :

$myString = "[" + implode(", ", $myArray) + "]";

That will give you the string "[1, 2, 3, 4, 5]"

It's an example but you got the idea.

Documentation about implode

Emrys Myrooin
  • 2,179
  • 14
  • 39
  • Yeah, but it won't be saved as an array. So you it's more complicated to get it back. – Haudegen Aug 05 '15 at 11:02
  • You can't just store an array in a MySql databse. You have to push it as string. That syntax here is JSON syntax. You can Use php Json functions for more simple use – Emrys Myrooin Aug 05 '15 at 11:05
  • this will not save the array as an array, this is not the answer to the question – Sebastian Utz Aug 05 '15 at 11:24
  • hey guys, this is not about MySQL, please read the question again. believe me or not, other databases than MySQL do really exist ;) – Sebastian Utz Aug 05 '15 at 11:37
  • I honestly think @EmrysMyrooin knows that, too. I just wanted to point out this answer. That doesn't mean we don't know there are other DBs, it just means we correct each other so other will benefit from that and thats what this platform is here for, not? So, Sebastian Utz and Emrys Myrooin thank you for sharing your knowledge with me. ;) – Haudegen Aug 05 '15 at 11:42
  • @Sebastian Utz Yes I haven't right understood what he was asking. Thanks for the corection. – Emrys Myrooin Aug 05 '15 at 12:19
  • It is a DB that allows arrays to be inserted, just the PDO class i'm using seems like its missing something. Thanks though. – Blowfish Tech Aug 05 '15 at 12:43
-1

I haven't worked with Crate, but I would do something like this:

$myArray = ['1', '2', '3', '4', '5'];
$myArrayEncoded = json_encode($myArray);

$db = new Database;
$db->Query("insert into tbl (id, arrayCol) values (?,?)");
$db->bind(1, '1');
$db->bind(2, $myArrayEncoded); 
$db->execute();

When you later retrieve it from the Database and want to have it back like it is on the first line, you decode it:

$myArrayDecoded = json_decode($myArrayEncoded);

Maybe there are better solutions from people who actually worked with it, but I am pretty sure, this should work too!

Haudegen
  • 498
  • 1
  • 4
  • 17
  • this will not make use of the array type, this is not the answer to the question – Sebastian Utz Aug 05 '15 at 11:25
  • As I said it in my answer, I've never worked with it and this is just a workaround. It should work, so IMO need to give -1! So, if you have a better solution, then just post it! (As you did! So thank you for correcting it!) – Haudegen Aug 05 '15 at 11:38
  • Its a DB that allows array to be inserted, thanks though. – Blowfish Tech Aug 05 '15 at 12:42
-3

use

$statement->bindValue(2, [1, 2], PDO::PARAM_ARRAY)
Adnan Zafar
  • 108
  • 7