Php-mysql Course

In this tutorial you can learn how to work with NULL in MySQL tables: Insert, Update, and Select columns with NULL value.

Add column with NULL value

To insert into a MySQL table rows with columns having NULL, add the NULL value without quotes.
$sql = "INSERT INTO `table_name`
 (`column1`, `column2`, `column3`)
 ('val1', NULL, 'val3')";

Or, if the NULL value is stored into a variable, add "NULL" as a string to that variable, then write the variable into the SQL query, without quotes.
$nul = 'NULL';
$sql = "INSERT INTO `table_name`
 (`column1`, `column2`, `column3`)
 ('val1', $nul, 'val3')";

- If you create the $nul value like this:   $nul = NULL; , the SQL query will contain nothing in the place where this variable is added. Will result:
INSERT INTO `table_name` (`column1`, `column2`, `column3`) VALUES ('val1', , 'val3')

- If you want to insert the string "NULL", add it within quotes:
$sql = "INSERT INTO `table_name`
 (`column1`, `column2`, `column3`)
 ('val1', 'NULL', 'val3')";

UPDATE with NULL value

The same works with UPDATE.
$sql = "UPDATE `table_name` SET `column1`='val1', `column2`=NULL, WHERE `column3`='val3'";
$nul = 'NULL';
$sql = "UPDATE `table_name` SET `column1`='val1', `column2`=$nul, WHERE `column3`='val3'";

SELECT fields with NULL value

To select rows in a MySQL table according to columns with NULL value, use IS NULL.
$sql = "SELECT * FROM `table_name` WHERE `column` IS NULL";

If you want to Not return the rows with a specific NULL field, use IS NOT NULL.
$sql = "SELECT * FROM `table_name` WHERE `column` IS NOT NULL";

Daily Test with Code Example

Click on the tag that creates a cell in table
<tr> <span> <td>
Indicate the CSS property used to specify a background image for an element
background-color background-image color
h3 {
  background-image: url("image.jpg");
What instruction can be used to parse all the array items?
for() [) object()
var arr = [1, "ab", ""];
for(var i=0; i< arr.length; i++) { alert(arr[i]); };
Indicate the PHP instruction used to traverse an associative array.
for() foreach() if()
$arr =["k1"=>"v1", "k2"=>"v2", "k3"=>"v3");
foreach($arr AS $k => $v) { echo "<br/>". $k ." - ". $v; }
Insert, Select and Update NULL value in MySQL

Last accessed pages

  1. Create simple Website with PHP (28921)
  2. Script Users Register, Login, Online (17461)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (49205)
  4. PHP MySQL - WHERE and LIKE (23337)
  5. PHP OOP - Constructor Method (5337)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (813)
  2. querySelector and querySelectorAll (438)
  3. Read Excel file data in PHP - PhpExcelReader (404)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (380)
  5. JavaScript Course - Free lessons (293)