0

I try save integer with value 0 in php to decimal(7,2) in miranda db.

But integer with value 0 save always 99999.99 I cannot find the solution. I think it can transform automatically. I use PDO in php.

Other values than 0 works well.

My inserted array:

array(":name"=>"3ld",":urlid"=>($url->id),":date"=>"NOW()",":type"=>"renew",":status"=>($url->alias.$url->custom.".".$url->tld),":price"=>$price)

Var dump value from array:

array(6) { [":name"]=> string(3) "3ld" [":urlid"]=> string(1) "1" [":date"]=> string(5) "NOW()" [":type"]=> string(5) "renew" [":status"]=> string(19) "support.url.tld" [":price"]=> int(0) }

The part of code:

$this->db->insert("log_url",array(":name"=>"3ld",":urlid"=>($url->id),":date"=>"NOW()",":type"=>"renew",":status"=>($url->alias.$url->custom.".".$url->tld),":price"=>$price));

DB insert function:

    public function insert($table,$parameters=array()){
    $param="";
    $val="";
    $insert= $this->ph($parameters);
    //Build Query
    $query="INSERT INTO {$this->dbinfo["prefix"]}$table";                       
    if(is_array($insert)){
        $count=count($insert);
        $i=0;           
        foreach ($insert as $key => $value) {
            if($parameters[$value]=="NOW()"){
                $val.= "NOW()";
                unset($parameters[$value]);
            }else{
                $val.=$this->quote($value,$parameters);
            }                   
            $param.="`$key`";
            if(++$i != $count) {
                $param.=",";
                $val.=",";
            }               
        }
        $query.=" ($param) VALUES ($val)";
    }       
    $result = $this->db->prepare($query);
    $result->execute($parameters);
    if($this->error_message($result->errorInfo())) {
        $this->query=strtr($query,$parameters);
        $this->db_error=$this->error_message($result->errorInfo());
        exit;
    }
    ++$this->num_queries;
return TRUE;        
}

Quote function:

private function quote($string,$param=''){  
    if(empty($param)){
        return "'$string'";
    }
    return $string;
}

Generate placeholders function:

private  function ph(array $a){
    $b=array();
    foreach ($a as $key => $value) {
        $b[str_replace(":", "", $key)]="$key";
    }
    return $b;
}

Any information helps, thank you.

Updated Code:

public function insert($table,$parameters=array()){
    $param="";
    $val=array();
    $insert= array_keys($parameters); var_dump($parameters);
    //Build Query
    $query="INSERT INTO {$this->dbinfo["prefix"]}$table";

    if(is_array($insert)){

        $query.=' (`'.implode($insert,"`,`").'`) VALUES (:'.implode($insert,', :').')';

        $result = $this->db->prepare($query);

        foreach($parameters as $key=>$param) {
          $result->bindParam(":".$key, ($param['value']=='NOW()')?date('Y-m-d H:i:s'):$param['value'], PDO::PARAM_STR);
        }
    }      

    $result->execute(); //$result->execute($parameters);
    if($this->error_message($result->errorInfo())) {
        $this->query=strtr($query,$parameters);
        $this->db_error=$this->error_message($result->errorInfo());
        exit;
    }
    ++$this->num_queries;
return TRUE;        
}

produce as price 2015.00

14
  • show us code fragment please Commented Jan 10, 2015 at 13:08
  • i update me question Commented Jan 10, 2015 at 13:16
  • that is not fragment of code, it i snot even one line of code :-) could you please show the fragment that shows your logic and data manipulation? 2-3-5-10 lines?? Commented Jan 10, 2015 at 13:19
  • Not sure what's the question, did you try to cast that variable? Commented Jan 10, 2015 at 13:23
  • I update the code. There is one function to insert in DB. The variable for price is integer number, but when is it zero problem is there. Other values works well. Commented Jan 10, 2015 at 13:32

1 Answer 1

1

Replace call to method by removing ":" it has no sense to send ":" and then remove that ":" by ->ph()

$this->db->insert("log_url",
    array("name"=>array("value"=>"3ld","type"=>PDO::PARAM_STR), //
      "urlid"=>array("value"=>$url->id,"type"=>PDO::PARAM_STR),
      "date"=>array("value"=>'NOW()',"type"=>PDO::PARAM_STR),     
"type"=>array("value"=>'renew',"type"=>PDO::PARAM_STR),
"status"=>array("value"=>$url->alias.$url->custom.".".$url->tld,"type"=>PDO::PARAM_STR),
"price"=>array("value"=>$price,"type"=>PDO::PARAM_STR)));       //

about PDO types params read here: http://php.net/manual/en/pdo.constants.php and here http://php.net/manual/en/pdostatement.bindparam.php

and inside your function you can replace this:

$param="";
    $val="";
    $insert= $this->ph($parameters);
    //Build Query
    $query="INSERT INTO {$this->dbinfo["prefix"]}$table";                       
    if(is_array($insert)){
        $count=count($insert);
        $i=0;           
        foreach ($insert as $key => $value) {
            if($parameters[$value]=="NOW()"){
                $val.= "NOW()";
                unset($parameters[$value]);
            }else{
                $val.=$this->quote($value,$parameters);
            }                   
            $param.="`$key`";
            if(++$i != $count) {
                $param.=",";
                $val.=",";
            }               
        }
        $query.=" ($param) VALUES ($val)";
    }       
    $result = $this->db->prepare($query);

with this:

$val=array();
$insert= array_keys($parameters);
//Build Query
$query="INSERT INTO {$this->dbinfo["prefix"]}$table";                       
if(is_array($insert)){

    $query.=' (`'.implode($insert,"`,`").'`) VALUES (:'.implode($insert,', :').')';

    $stmt= $this->db->prepare($query);

    foreach($parameters as $key=>$param) {
      //$stmt->bindParam(":".$key, ($param['value']=='NOW()')?date('Y-m-d H:i:s'):$param['value']);
      if($param['value']=='NOW()') {
         $now = date('Y-m-d H:i:s');
         $stmt->bindParam(":".$key, $now, $param['type']); 
      } else {
         $stmt->bindParam(":".$key, $param['value'], $param['type']); 
      }
    }
}       

so, this must work

by the way, don't forget change:

 $result->execute($parameters);

to

 $stmt->execute();

downthere...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.