RE: [PHP-DB] Why NULL?

2002-02-01 Thread Zach Curtis

Yes, I did not receive any replies to that message yesterday. Here is my
code (condensed) as well. What ends up happening is that when I write a
variable for a field that did have data, for example "cus034a", to the db it
shows a value of "0" when I intended it to be "NULL".

Thanks.


Zach


# partial table definition
username CHAR(6) NOT NULL,
password CHAR(8) NOT NULL,
int_id VARCHAR(4) NOT NULL,
cus034a TINYINT UNSIGNED NULL,
cus034b TINYINT UNSIGNED NULL,
cus034c TINYINT UNSIGNED NULL,
sat01 TINYINT UNSIGNED NULL,
PRIMARY KEY(password, int_id)


// php script

// INITIALIZE DATA ARRAY
function initialize_data()
{
// create array to store record
$data_array = array();
$data_array["username"] = "";
$data_array["password"] = "";
$data_array["int_id"] = "";
$data_array["cus034a"] = NULL;
$data_array["cus034b"] = NULL;
$data_array["cus034c"] = NULL;
$data_array["sat01"] = NULL;
...
...
...

return $data_array;
}

// CREATE ARRAY TO HOLD FLAT FILE
$file_array = array();
$file_array = file(DAT_FILE);

$count = count($file_array);
if ($count == 0)
echo "No records found in dat.cgi file.";

// initialize data array
$data_array = initialize_data();

// $i is the current element in the $file_array

// LOOP THRU FLAT FILE
while ($i < $count)
{
// extract header data
$data_array["username"] = trim(substr($file_array[$i], 0, 12));
$data_array["password"] = trim(substr($file_array[$i], 12, 8));
$data_array["int_id"] = trim(substr($file_array[$i], 20, 4));

// extract response data
for ($j = 0; $j < $data_array["num_responses"]; $j++)
{
$i++;
$extract_array = explode(",", $file_array[$i]);

if ($extract_array[0] == "cus034a")
{
$data_array["cus034a"] = $extract_array[1];
}
elseif ($extract_array[0] == "cus034b")
{
$data_array["cus034b"] = $extract_array[1];
}
elseif ($extract_array[0] == "cus034c")
{
$data_array["cus034c"] = $extract_array[1];
}
elseif ($extract_array[0] == "sat01")
{
$data_array["sat01"] = $extract_array[1];
}
...
...
...
else
{
echo "Could not process response data for int_id: $int_id" .
 "and password: $password
recorded.";
}
} // end for

// WRITE RECORD TO DB TABLE
$date_time = date("Y-m-d H:i:s");
$result2 = mysql_query("INSERT INTO s999dat SET
username= '". 
$data_array["username"] ."' ,
password= '". 
$data_array["password"] ."' ,
int_id= '". 
$data_array["int_id"] ."' ,
cus034a= '". 
$data_array["cus034a"] ."' ,
cus034b= '". 
$data_array["cus034b"] ."' ,
cus034c= '". 
$data_array["cus034c"] ."' ,
sat01= '". 
$data_array["sat01"] ."' ,
...
...
...
);
if (mysql_affected_rows() == 0)
{
echo "Error adding record to db." .
 "int_id: $int_id" .
 "and password: $password" .
  mysql_error() . "";
exit;
}

$i++;
// INITIALIZE DATA ARRAY
$data_array = initialize_data();

} // end while


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP-DB] Why NULL?

2002-02-01 Thread Rick Emery

It appears that as you read each single field value from the flat file, you
must determine if it has a value.  If it has no value, do not insert a value
into the NULLed array.

I noticed this is the same problem you poseted yesterday.  If you show us
your code, we might be able to help.  

-Original Message-
From: Zach Curtis [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 01, 2002 9:26 AM
To: [EMAIL PROTECTED]
Subject: [PHP-DB] Why NULL?


I have a loop process that reads a record out of a flat file, stores the
data for a given record in an array, writes the record to the db, resets the
data in the array, then iterates again thru the loop. Not every record has a
value for each field. For example,

Not every record has a value for the field "cus034a". Since this is numeric
type data, I do not want the value in the db to be "0" if there is no value
for the field (it should be NULL).

# partial table definition
cus034a TINYINT UNSIGNED NULL,
sat01 TINYINT UNSIGNED NULL,

What I try to do is set the array to NULL values before iterating the loop
again. However, if I do this and the next record does not have a value for
"cus034a", the value in the db still ends up being "0".

$data_array["cus034a"] = NULL;
$data_array["sat01"] = NULL;

I must be doing something wrong?

Thank you.


Zach Curtis
POPULUS


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]