【发布时间】:2016-08-26 08:04:54
【问题描述】:
我的 CRUD 类中有以下代码
//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();
}
我从我的索引页面调用这个方法,如下所示:
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
//向table_1表插入值
无论是否存在任何记录,这总是返回true? 这段代码出了什么问题?
【问题讨论】:
-
查看以下链接 php.net/manual/en/mysqli.error.php 和 php.net/manual/en/function.error-reporting.php 并将其应用于您的代码。
-
页面没有显示错误,我添加了error_reporting(E_ALL); ini_set("display_errors", 1);在我的索引页面上