dongyongkui6329 2015-07-10 14:37
浏览 95
已采纳

PHP数据库连接类bind_param

I would like to write a database connection class and I dont understand how I have to write the select method with bind_param-s. Here is the full code. And here the part of the code where I need the help:

public function select($sql){
        $db = $this->connect(); //This methos connect to the DB
        $stmt = $db->prepare($sql); 
        if($stmt === false){ //If the prepare faild
            trigger_error("Wrong SQL", E_USER_ERROR);
        }
        $error = $stmt->bind_param("i", $id);
        if($error){
            return "Error: ".$stmt->error, $stmt->errno;
        }
        $err = $stmt->execute();
        if($error){
            return "Error: ".$stmt->error, $stmt->errno;
        }
        $result =  $stmt->bind_result($id);
        $stmt->close();
        $dbConnection->closeConnection($db);
        return $result;
    }

I need to got it parameters or how can I slove it?

  • 写回答

1条回答 默认 最新

  • douzhuo3233 2015-07-10 14:51
    关注

    You need to pass your values into this function too. And eventually bind them into prepared statement.
    Optionally you can pass string with types, but by default all "s" will do.

    Also remember that you should connect only ONCE per script execution. and then use one single connection all the way throughout your code.

    And get rid of all these error checks. Set mysqli in exception mode instead.

    public function q($sql, $values = array(), $types = NULL)
    {
        $stm = $this->mysql->prepare($sql);
        if (!$types)
        {
            $types = str_repeat("s", count($values));
        }
    
        if (strnatcmp(phpversion(),'5.3') >= 0)
        {
            $bind = array();
            foreach($values as $key => $val)
            {
                $bind[$key] = &$values[$key];
            }
    
        } else {
    
            $bind = $values;
        }
    
        array_unshift($bind, $types);
        call_user_func_array(array($stm, 'bind_param'), $bind);
        $stm->execute();
        return $stm->get_result();
    }
    

    so it can be used like this

    $res = $db->q("SELECT name FROM users WHERE id=?", [$id]);
    

    or

    $res = $db->q("SELECT name FROM users WHERE id=?", [$id], "i");
    

    your other functions have to be changed as well.

    class DB{
    
        public $con;
    
        function __construct()
        {
             $this->con = new mysqli("localhost", "root", "", "proba_fferenc");
        }
    
        public function select(...)
        {
            // as shown above
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私