⟩ PHP MSSQL - How To Insert Data with NULL Values?
There are two ways to provide NULL value to a column in an INSERT statement:
* Include the column in the statement, but specify keyword NULL as the value.
* Exclude the column from the statement.
The following tutorial exercise inserts two rows. Both of them have NULL values. But they were inserted differently:
<?php
$con = mssql_connect('LOCALHOST','sa','GlobalGuideLine');
mssql_select_db('GlobalGuideLineDatabase', $con);
$sql = "INSERT INTO ggl_links"
. " (id, url, notes, counts, time)"
. " VALUES (101, 'www.GlobalGuideLine.com',"
. " NULL, NULL, NULL)";
$res = mssql_query($sql,$con);
if (!$res) {
print("SQL statement failed with error: ");
print(" ".mssql_get_last_message()." ");
} else {
print("One data row inserted. ");
}
$sql = "INSERT INTO ggl_links (id, url) VALUES ("
. " 102, 'www.GlobalGuideLine.com')";
$res = mssql_query($sql,$con);
print("Another data row inserted. ");
mssql_close($con);
?>
If you run this script, you will get:
One data row inserted.
Another data row inserted.