保存字符串的最后10位数

时间:2016-05-04 08:55:57

标签: php mysql

我目前正在使用api将我的用户的手机号码保存到数据库

public function save_user_phonebook_record($data)
{
    $mobile=str_replace('+','0',$data['mobile']);
    $mobile=str_replace(' ','',$mobile);
    $mobile=substr($mobile, -10);
    $params = array(':mobile' => $mobile, ':user_id' => $data['user_id']);
    $sql = "SELECT * FROM friends where mobile=:mobile AND user_id=:user_id";
    $stmt = parent::query($sql, $params);
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
        $res=$row;
    endwhile;
    if(count($res)<1)
    {
        $sql = 'INSERT INTO friends (user_id, name, mobile) 
        VALUES(:user_id, :name, :mobile)';
        $params =(array(
            ':user_id' => $data['user_id'],
            ':name' => $data['name'],
            ':mobile' => $data['mobile']
        ));
        parent::query($sql, $params) or die(parent::$dbh->errorInfo());
    }
}

API USED

$client->save_phonebook(array('key'=>$api_key, 'user_id'=>'25349', 'phonebook'=>json_encode(array(array('name'=>'XXXXX Jain',"mobile"=>"+919876543210"),array('name'=>'xxxxxx Verma',"mobile"=>"09876543210")))));

存储在表格移动设备中的手机号码保存为+919812345678,09876543210,,但我已将$mobile=substr($mobile, -10);仅保存该号码的最后10位数字,但由于某种原因似乎无法正常工作

2 个答案:

答案 0 :(得分:1)

您需要在此处进行更改,将$data['mobile']替换为$mobile

$params =(array(
            ':user_id' => $data['user_id'],
            ':name' => $data['name'],
            ':mobile' => $mobile
        ));

答案 1 :(得分:0)

您正在$mobile

上应用substr()

使用$mobile代替$data['mobile']

或者,substr()上的$data['mobile']

$data['mobile'] = substr($data['mobile'], -10);