skip to Main Content

In my Laravel project (version 5.3), I’m sending a param in a URL:

&container=0

But by the time it gets into the PHP function that creates an SQL query, it treats the zero as empty and satisfies the empty string portion of this line:

$containerCL = empty($container) ? '' : "AND SHPTABLE.CNT IN (".implode(',',$container).")";

I do want to send an empty string into the query if no option is chosen for this, but in this case, I need to send the zero value as we have some records identified by that being the actual value. How can I let this catch empty parameters but still use zero as an actual value for the query?

2

Answers


  1. It seems like the issue is with the implode function when $container is 0. The implode function expects an array, but if $container is 0, it’s not an array and thus implode returns an empty string, causing the syntax error.

    You can check if $container is an array before calling implode. You can cast it to an array if it’s not an array. This will ensure that implode always receives an array, even if $container is 0.

    $containerCL = !isset($container) ? '' : "AND SHPTABLE.CNT IN (".implode(',', (array)$container).")";
    
    Login or Signup to reply.
  2. make use of isset() function.
    $containerCL = isset($container) ? ” : "AND SHPTABLE.CNT IN (".implode(‘,’,$container).")";

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search