Saturday, 7 September 2013

PHP/MYSQL - Inserting array values to db via mysqli prepared statement

PHP/MYSQL - Inserting array values to db via mysqli prepared statement

Using a mysqli prepared statement I would like to insert an array into a
mysql database table.
Being aware that bind-param and arrays do not go together, we would like
to write the query in php first, then process this as a prepared
statement:
$tagQuery = "INSERT INTO word_tags(speaks) VALUES ";
// Count total array values
$icoderayInsideCount = count($icoderayInside);
foreach ($icoderayInside as $icoderayInsideKey=>$icoderayInsideValue)
{
// Last value
// Currrent Array Key Total / Last Array Value
if ($icoderayInsideKey == $icoderayInsideCount)
{
$tagQuery .= "('$icoderayInsideValue')";
}
// All other values
else
{
$tagQuery .= "('$icoderayInsideValue'), ";
}
}
// Send array (keywords) to database
if ($stmt2 = $link2->prepare($tagQuery))
{
if (!$stmt2->execute())
{
// #2 If it can prepare but can't execute, why?
echo "Error {$link2->errno} : {$link2->error} (Cant execute?)
<br/>";
// Dump query to check the end result
var_dump($tagQuery);
exit();
}
$stmt2->close();
}
else
{
// #1 If it cant prepare, why?
echo "Error {$link2->errno} : {$link2->error} (Cant prepare?)";
exit();
}
When i run this via PHP / Server i get:
Error 1064 : You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'('DONGLES'),' at line 1 (Cant prepare?)
So, the statement prepares but is not being executed.
The value of the generated query, $tagQuery is:
INSERT INTO word_tags(speaks) VALUES ('PMP3670B_BK'), ('PRESTIGIO'),
('MULTIPAD'), ('7"'), ('800X480'), ('1GHZ'), ('ARM'), ('CORTEX'), ('A8'),
('CPU'), ('512MB'), ('DDR3'), ('DRAM'), ('ANDROID'), ('4.1'), ('JELLY'),
('BEAN'), ('MALI'), ('400'), ('MP'), ('GPU'), ('VIDEO'), ('4GB'),
('INTERNAL'), ('FLASH'), ('MEMORY'), ('SUPPORT'), ('32GB'), ('SDHC/SD'),
('USB/WI-FI/HEADSET'), ('PORT'), ('LITHIUM'), ('POLYMER'), ('BLACK'),
('HDMI'), ('OUTPUT'), ('UPTO'), ('1080'), ('HD'), ('USB2.0'), ('MINI'),
('HIGH'), ('SPEED'), ('FOR'), ('3G'), ('DONGLE'), ('OTG'), ('CABLE'),
('INCLUDED')
The fourth value from the end is('DONGLE') which is what the error message
is complaining about.
When i run this exact same query through phpmyadmin there is no error
involved.
What i assume is happening, is that there is some kind of length limit
involved within creating a prepared statement... Or something to this
effect.
Have scratched my brains for hours now to try to solve this and have not
found any relating information.
If anyone could offer some assistance / advice / indication / input or
otherwise as to what the conflict of problem may be within this, it would
be GREATLY appreciated.
Thanks so much for the time and effort in readying through this!

No comments:

Post a Comment