douji1077 2016-05-01 14:00
浏览 43
已采纳

用于INSERT,UPDATE,DELETE和Search的Php Mysqli简单类方法

I have the following code in my CRUD class

//function to execute prepared statement query
//$sql = select , insert, update and or delete query => insert into table(col,col,col,...col) values(?,?,?,...?);
//$dataTypes = "ssidb", it could be any char in s=>string, i=>integer, d=>double and b=>blob data
//$param = $val1,$val2,$val3,....$valn, this is an option coma separated values to bind with query
public function dbQuery($sql,$dataTypes="",$param=""){
    try{
        $this->connect();
        $stmt = $this->con->stmt_init();
        $stmt = $this->con->prepare($sql);
        $stmt->bind_param($dataTypes, $param);          
        if($stmt->execute() === true){
            return true;    
            }
        else{
            return false;   
            }
        }catch(Exception $e){
            $this->errorMsg = $e->getMessage(); 
        }
        $this->closeConnection();
    }

I am calling this method from my index page like this:

 if(isset($_POST['btnSearch'])){
 //search for some record with primary key
 $sno = intval($_POST['sno']);
 $sql = "SELECT sno,std_name,email,roll_number FROM table_1 WHERE sno = ?";
 $dTypes = "i";
 $params = $sno;
if($db->dbQuery($sql,$dTypes,$params)){
    echo('Record exists');
    }
else{
    echo('Record did not found'.$db->errorMsg);
    }
}//search for record

//inserting values to table_1 table

This always return true either there is any record exists or not? Whats going wrong with this code?

  • 写回答

2条回答 默认 最新

  • dongmacheng3222 2016-05-02 04:25
    关注

    There are many flaws in your code, and it will never work as intended, even after fixing this particular problem.

    Before starting with a class, you need to practice heavily with raw API functions, and learn how to use them by heart. Otherwise your class will be just a straw house that will crumble from a softest touch.

    Now to your problem.

    To solve it, you need to understand one very important mathematical conception, that reads "empty result is not an error". 10 - 5 - 5 = 0 doesn't mean there is an error in your calculations! It merely means that the result is zero.

    Exacly the same is here. When a database returns no rows, it doesn't mean there is an error. It just meams that there is zero (no) data to return.

    The opposite is true as well: if there is no error, it doesn't mean that there are rows found.

    To see whether any row were returned or not, you need to fetch this very row.

    Therefore, instead of checking execute() result, just fetch your row into a variable and then check whether it contains anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题