Jump to content

sprintf will not keep number 2 digits


project3

Recommended Posts

 

$fieldList["`id_dt`"] = sprintf('%02d',"02");

 

the above gets put into an array an added to db. it will not stay 2 digits.

I need the 0 to stay in front and will not stay there.

 

// Insert

$sSql = "INSERT INTO `ticket` (";

$sSql .= implode(",", array_keys($fieldList));

$sSql .= ") VALUES (";

$sSql .= implode(",", array_values($fieldList));

$sSql .= ")";

phpmkr_query($sSql, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);

$fieldList["`sid`"] = phpmkr_insert_id($conn);

$result = (phpmkr_affected_rows($conn) > 0);

$x1 = $fieldList["`sid`"];

 

Link to comment
https://forums.phpfreaks.com/topic/91120-sprintf-will-not-keep-number-2-digits/
Share on other sites

I believe this depends on the data type of your field. If it's text, the zero should stay; otherwise, the leading zero will be removed unless you're using the ZEROFILL option.

 

Field  Type        Collation              Attributes 

id_dt varchar(11)    latin1_general_ci None

What does $sSql look like before the insertion?

 

see the 02 is in the insert but for some reason the db cuts it off.

 

INSERT INTO `ticket` (`id_dt`,`clientid`,`caselink`,`supportid`,`engineerid`,`additional_email`,`summary`,`time_in`,`time_out`,`jobdesc`,`work`,`cur_status`,`further_action`,`previous_notes`,`equipments_order`,`items_to_order`,`signature`,`company_position`,`bill_status`,`bill_ref`,`case_closed`,`time_in_secs`,`time_out_secs`) VALUES (02,71,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'NO',NULL,NULL,'NO',NULL,NULL,NULL,NULL,'NO',NULL, '- Mins -')

 

What does $sSql look like before the insertion?

 

 

Just to make sure i changed the field from varchar to text and that

did not make a difference.

 

see the 02 is in the insert but for some reason the db cuts it off.

 

 

INSERT INTO `ticket` (`id_dt`,`clientid`,`caselink`,`supportid`,`engineerid`,`additional_email`,`summary`,`time_in`,`time_out`,`jobdesc`,`work`,`cur_status`,`further_action`,`previous_notes`,`equipments_order`,`items_to_order`,`signature`,`company_position`,`bill_status`,`bill_ref`,`case_closed`,`time_in_secs`,`time_out_secs`) VALUES (02,71,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'NO',NULL,NULL,'NO',NULL,NULL,NULL,NULL,'NO',NULL, '- Mins -')

 

what is phpmkr? it is probably the one cutting off the 0. look in that code for an is_numeric test.

 

I took that out and put my own sql in. same result.

 

$this_one = sprintf('%02d',"02");

 

$con = mysql_connect($host,$dbuser,$dbpass);

mysql_select_db($thedb, $con);

mysql_query("INSERT INTO `ticket` (id_dt) VALUES ($this_one)");

 

it needs to look like this

 

VALUES ('02',71, ...

 

with '..' if you want it treated as a string value

 

 

awesome that worked great!

 

but I guess I cant add ' onto this

 

  // Insert

      $sSql = "INSERT INTO `ticket` (";

      $sSql .= implode(",", array_keys($fieldList));

      $sSql .= ") VALUES (";

      $sSql .= implode(",", array_values($fieldList));

      $sSql .= ")";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.