This is my first post on this site so I'm sorry in advance if some things come out weird in the formatting :)
After about a week of searching the internet for answers to this problem I figured I'd give this website a try as its helped me countless times in the past.
Anyways, I recently started using PDO in PHP to interact with a MS SQL database on a project I've been working on. The way one of the tables is set up is essentially almost all numeric, datetime, and character fields. My issue is with the date fields. The script I'm writing is made to copy a row from one table and put it in another with its own ID - I was getting errors because the original table has NULL values as the data in that table changes a lot and the default for many of the fields are NULL.
To fix that problem, afer trying quite a lot of different things to get the NULL values to work, I wrote a function that replaces all null values with zeroes. This works fine except for the date values - after commenting them out the insert statement works as it should - however with them in I get the "Conversion failed when converting datetime from character string." error, presumably because the database cannot convert '0' into a datetime value.
How would I go about fixing this?
This is my function that replaces NULL values. I feel I should also note that if I just keep the values NULL I get a Error converting data type nvarchar to numeric. error.
function check_empty($field, $slno){
try{
require("assets/dbcnct.php");
$stmt = $DBH->prepare(" select ". $field ." from timeticket where slno = :slno ");
$stmt->bindParam(':slno', $slno);
$stmt ->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo "<pre>";
var_dump($result);
echo "</pre>";
foreach($result as $key=>$val){
if(empty($val)){
$val = 0;
}
}
return trim($val);
}
catch(PDOException $e) {
echo "<pre>";
echo $e->getMessage();
echo "</pre>";
}
}
Welcome to Stackoverflow! :-)
Setting default values for NULL
values
You can do
SELECT IFNULL(`myColumn`, 'default value for this column') as `myColumn` ...
to set a default value for a column, if it is NULL
. This will return the value of myColumn
if it is not NULL
and default value for this column
otherwise.
Example: Get the current date if the date in the table is NULL
SELECT IFNULL(`myDateColumn`, NOW()) as `myDateColumn` ...
Copying tuples between tables in MySQL
I also think that you don't need PHP to copy tuples from one table into another. See MySQL's INSERT INTO SELECT.
Example: Assuming you have source
and destination
both with an auto-incrementing primary key id
and a column date
which allows NULL
values.
INSERT INTO `destination` (`id`, `date`) SELECT NULL, `date` FROM `source`
This will select all date
s from the table source
and insert them into destination
creating a new ID, for each newly created tuple. This will also preserve NULL-values.
This is the preferred solution for copying tuples between tables. Because no data is actually transferred between MySQL and PHP, this will be very fast, even for vast amounts of tuples.
NULL
? Do you need to replaceNULL
with empty strings or0
when copying, or could you just haveNULL
-values in your destination table, as well - Basti 2012-04-04 23:02