cancel
Showing results for 
Search instead for 
Did you mean: 

"Right truncation of string data" error on multiple inserts with prepared statements

Former Member
0 Kudos
31,632

I'm currently developing web app (PHP 7, Lumen framework 5) that communicates with SQL Anywhere database. I have succesfully created an SQL Anywhere database (version 17), and now I'm trying to insert data into database tables, but no luck. I have the following solution which works correctly (no errors during execution), but stores data into tables row by row:

$query = "INSERT INTO $table_name ($cols) " .
    "VALUES (" . implode(",", array_fill(0, $cols_count, "?")) . ")";

foreach ($rows as $row) {
    $stmt = sasql_prepare($conn, $query);

    foreach (array_values($row) as $index => &$value) {
        sasql_stmt_bind_param_ex(
            $stmt, $index, $value,
            $value ? gettype($value)[0] : 's',
            $value === null
        );
    }

    sasql_stmt_execute($stmt);
    sasql_stmt_close($stmt);
}

However, when I try the next solution I receive the following error "sasql_stmt_execute(): SQLAnywhere: [-638] Right truncation of string data" (logic stays the same, but rows are stored all at once):

$row_placeholder = implode(",", array_fill(0, $cols_count, "?"));
$placeholders = implode(",", array_fill(0, count($rows), "($row_placeholder)"));

$query = "INSERT INTO $table_name ($cols) VALUES $placeholders";

$stmt = sasql_prepare($conn, $query);

$param_number = 0;
foreach ($rows as $row) {
    foreach ($row as &$value) {
        sasql_stmt_bind_param_ex(
            $stmt, $param_number++, $value,
            $value ? gettype($value)[0] : 's',
            $value === null
        );
    }
}

sasql_stmt_execute($stmt);
sasql_stmt_close($stmt);

I don't know what can cause this error, as well as was not able to find a proper solution...Need help here.

Accepted Solutions (0)

Answers (0)