我的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表
无论是否存在任何记录,这总是返回? 这段代码出了什么问题?
答案 0 :(得分:2)
您的代码中存在许多缺陷,即使解决了这个特定问题,它也无法按预期工作。
在开始上课之前,您需要大量练习原始API函数,并学习如何使用它们。否则你的班级将只是一个稻草屋,会从最柔软的触摸中崩溃。
现在问题。
要解决这个问题,您需要了解一个非常重要的数学概念,即“空结果不是错误”。 10 - 5 - 5 = 0并不意味着您的计算中存在错误!它仅仅意味着结果为零。
在这里也是如此。当数据库不返回任何行时,并不意味着存在错误。它只是表示有零(无)数据要返回。
反之亦然:如果没有错误,那并不意味着找到了行。
要查看是否返回了任何行,您需要获取此行。
因此,不是检查execute()结果,只需将行提取到变量中,然后检查它是否包含任何内容。
答案 1 :(得分:-2)
<html>
<p>
/*
error_reporting(0);
$IP = $HTTP_SERVER_VARS["REMOTE_ADDR"] ;
$vhttpreferrer = "";
if (isset($HTTP_SERVER_VARS["HTTP_REFERER"]))
{
$vhttpreferrer = $HTTP_SERVER_VARS["HTTP_REFERER"];
}
class myclass
{
var $DBASE = "DB_NAME";
var $USER = "root";
var $PASS = "";
var $SERVER = "localhost";
function SetPageSize($size){
$PAGELIMIT = $size;
}
function myclass()
{
$user = $this->USER;
$pass = $this->PASS;
$server = $this->SERVER;
$dbase = $this->DBASE;
$conn = mysqli_connect($server,$user,$pass,$dbase);
if(!$conn) {
echo "<br>Connection attempt failed<br>";
//$this->error("Connection attempt failed");
}
if(!mysqli_select_db($dbase,$conn)) {
$this->error("Dbase Select failed");
}
$this->CONN = $conn;
return true;
}
function close()
{
$conn = $this->CONN ;
$close = mysqli_close($conn);
if(!$close) {
$this->error("Connection close failed");
}
return true;
}
function error($text)
{
$no = mysqli_errno();
$msg = mysqli_error();
return $no;
}
function select ($sql="")
{
if(empty($sql)) { return false; }
if(!preg_match("/^select/i",$sql))
{
echo "wrongquery<br>$sql<p>";
echo "<H2>Wrong function silly!</H2>\n";
return false;
}
if(empty($this->CONN)) { return false; }
$conn = $this->CONN;
$results = @mysqli_query($conn,$sql);
if( (!$results) or (empty($results)) ) {
return false;
}
$count = 0;
$data = array();
while ( $row = mysqli_fetch_array($results))
{
$data[$count] = $row;
$count++;
}
mysqli_free_result($results);
return $data;
}
function affected($sql="")
{
if(empty($sql)) { return false; }
if(!preg_match("/^select/i",$sql))
{
echo "wrongquery<br>$sql<p>";
echo "<H2>Wrong function silly!</H2>\n";
return false;
}
if(empty($this->CONN)) { return false; }
$conn = $this->CONN;
$results = @mysqli_query($conn,$sql);
if( (!$results) or (empty($results)) ) {
return false;
}
$tot=0;
$tot=mysqli_affected_rows();
return $tot;
}
function insert ($sql="")
{
if(empty($sql)) { return false; }
if(!preg_match("/^insert/i",$sql))
{
return false;
}
if(empty($this->CONN))
{
return false;
}
$conn = $this->CONN;
$results = @mysqli_query($conn,$sql);
if(!$results)
{
$this->error("<H2>No results!</H2>\n");
return false;
}
$id = mysqli_insert_id($conn);
return $id;
}
function insertarray ($sqlarray,$tblname)
{
if(empty($this->CONN))
{
return false;
}
$conn = $this->CONN;
$field ='';
$fieldvalue ='';
foreach($sqlarray as $key=>$value){
$field .= $key.',';
$fieldvalue .= "'".$value."',";
}
$field =trim($field,',');
$fieldvalue =trim($fieldvalue,',');
$sql="insert into ".$tblname." (int_Id,".$field.")values(NULL,".$fieldvalue.") " ;
$results = @mysqli_query($conn,$sql);
if(!$results)
{
$this->error("<H2>No results!</H2>\n");
return false;
}
$id = mysqli_insert_id($conn);
return $id;
}
function selectjson ($sql="")
{
if(empty($sql)) { return false; }
if(!eregi("^select",$sql))
{
echo "wrongquery<br>$sql<p>";
echo "<H2>Wrong function silly!</H2>\n";
return false;
}
if(empty($this->CONN)) { return false; }
$conn = $this->CONN;
$results = @mysqli_query($conn,$sql);
if( (!$results) or (empty($results)) ) {
return false;
}
$count = 0;
$data = array();
while ( $row = mysqli_fetch_assoc($results))
{
$data[$count] = $row;
$count++;
}
mysqli_free_result($results);
return $data;
}
function edit($sql="")
{
if(empty($sql)) { return false; }
if(!preg_match("/^update/i",$sql))
{
return false;
}
if(empty($this->CONN))
{
return false;
}
$conn = $this->CONN;
$results = @mysqli_query($conn,$sql);
if(!$results)
{
$this->error("<H2>No results!</H2>\n");
return false;
}
$rows = 0;
$rows = mysqli_affected_rows();
return $rows;
}
function sql_query($sql="")
{ if(empty($sql)) { return false; }
if(empty($this->CONN)) { return false; }
$conn = $this->CONN;
$results = @mysqli_query($conn,$sql) or die("query fail");
if(!$results)
{ $message = "Query went bad!";
$this->error($message);
return false;
}
if(!preg_match("/^select/i",$sql)){
return true; }
else {
$count = 0;
$data = array();
while ( $row = mysqli_fetch_array($results)) {
$data[$count] = $row;
$count++;
}
mysqli_free_result($results);
return $data;
}
}
function delete($sql){
if(empty($sql)) { return false; }
if(empty($this->CONN)) { return false; }
$conn = $this->CONN;
$results = @mysqli_query($conn,$sql) or die("query fail");
return true;
}
}
if(!isset($obj)){
$obj = new myclass();
}
*/
<p>
</html>