M
M
Maxim Timofeev2018-02-27 22:38:59
PHP
Maxim Timofeev, 2018-02-27 22:38:59

How to defeat NULL when compiling a query in the database?

$valueString = join ( "','", $itemValues );
replaces null with ""

code next
$valueString = join ( "','", $itemValues );

      $valueString = "('" . $valueString . "'),";

      $values = "\n" . $valueString;

      if ($values != "") {
        $data_string = "INSERT INTO `$tableName` (`$items`) VALUES" . rtrim ( $values, "," ) . ";;;" . PHP_EOL;
        if ($this->fp)
          fwrite ( $this->fp, $data_string );
      }


accordingly I have
INSERT INTO `auth_item` (`name`,`type`,`description`,`rule_name`,`data`,`created_at`,`updated_at`) VALUES
('activity_stream_delete_comments','2','Delete comments' ,'','','1477591890','1477591890');;;
And in the database "" instead of null
Toli skis do not go, roofing felts it's time to sleep, you need a help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Melkij, 2018-02-27
@webinar

Accordingly, figure out how to correctly represent NULL in values ​​with NULL. This use of the join function will obviously have to be abandoned.
And it is better not to be perverted, and use pdo prepared statements, which null can be passed regularly.

M
Maxim, 2018-02-27
@pudovMaxim

'' !== NULL

S
synapse_people, 2018-02-27
@synapse_people

$valueString = '';

foreach($itemValues as $val) {
$valueString = is_null($val) ? 'null,' : sprintf('"%s",', $val);
}
$valueString = rtrim($valueString, ',');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question