[英]PHP CodeIgniter query error
這是我的控制器:
$isExist = $this->Sales_assistant_model->isUserExist($isUser);
$isUserExistInC_user = $this->Sales_assistant_model->checkCuserIs($sdata['email'], $bizId);
if ($isUserExistInC_user == 0) {
//If user in the C-user table
$isAlreadyRegistered = $this->Sales_assistant_model->checkUserEmailInC_user($sdata['email']);
if ($isAlreadyRegistered > 0) {
$businessName = $this->Sales_assistant_model->getBizName($bizId);
$roleName = $this->Sales_assistant_model->getUserRolesByRoleId($sdata['corporate_user_role_role_id']);
$username = $this->Sales_assistant_model->get_C_userNameByEmail($sdata['email']);
$to = $sdata['email'];
$subject = "New Business Role in".$businessName;
$body = "<b>Dear ".$username.",</b> You Have assigned as a ".$roleName." <br> in ".$businessName."<br /> Please Login with " . base_url();
//$this->Email_model->sendEmail($body, $to, "-", $subject);
$res = 1;
} else {
if ($isExist == 0) {
$res = $this->Sales_assistant_model->adduser($sdata);
$to = $isUser['email'];
$subject = "Confirm Registration";
$body = "<b>Dear ,</b> Please click here to confirm your registration . <br>
" . base_url() . "registration/registration?email=" . $isUser['email'] . "&code=" . $invtCode;
//$this->Email_model->sendEmail($body, $to, "-", $subject);
} else {
$status = 2;
$res_update = $this->Sales_assistant_model->updateUser_invite_rejectUser($sdata['email'], $bizId, $status);
$res = $this->Sales_assistant_model->adduser($sdata);
$to = $isUser['email'];
$subject = "Confirm Registration";
$body = "<b>Dear ,</b> Please click here to confirm your registration . <br>
" . base_url() . "registration/registration?email=" . $isUser['email'] . "&code=" . $invtCode;
//$this->Email_model->sendEmail($body, $to, "-", $subject);
}
}
} else {
$res = 0;
}
這些是我的模型函數:
public function checkUserEmailInC_user($email) {
$this->db->trans_start();
$this->db->select('COUNT(email) AS email');
$this->db->from('corporate_user');
$this->db->where('email',$email);
$this->db->where('status','1');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$row = $query->row();
$count = $row->email;
$count = 1;
} else {
$count = 0;
}
return $count;
}
public function get_C_userNameByEmail($email) {
$this->db->trans_start();
$this->db->select('cuser_name');
$this->db->from('corporate_user');
$this->db->where('email',$email);
$this->db->where('status','1');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$row = $query->row();
$count = $row->cuser_name;
}
return true;
}
如果注釋行
$isAlreadyRegistered = $this->Sales_assistant_model->checkUserEmailInC_user($sdata['email']);
內if
condtion作品。 該功能有什么錯誤?
例如,如果我為$isAlreadyRegistered
分配了0
,則內部if
條件起作用。
我想知道為什么會發生這種問題?
您的代碼有很多問題。
正如評論所問的:“為什么使用$ this-> db-> trans_start();?” 這些語句中不需要事務。 事務僅適用於“寫入”類型的查詢,例如插入和更新。 刪除$this->db->trans_start();
從您的模型方法。
您在代碼中重復很多。 遵循DRY (請勿重復)原則。 您的模型方法中有很多重復。
模型方法get_C_userNameByEmail($email)
應該返回用戶名。 相反,它總是返回TRUE。
這是Sales_assistant_model
修訂方法
checkUserEmailInC_user($email)
應該返回一個布爾值。 請注意如何使用受保護的函數runQuery($email)
來使代碼干燥。
public function checkUserEmailInC_user($email)
{
$this->db->select('email');
$query = $this->runQuery($email);
if($query->num_rows() > 0)
{
return TRUE;
}
//when an "if" block has a "return;", then there is no need for an "else" block
return FALSE;
}
下一個方法應返回用戶名。 但是,如果找不到記錄,則它必須返回其他內容。 NULL是一個很好的選擇,因為它很容易檢查。
public function get_C_userNameByEmail($email)
{
$this->db->select('cuser_name');
$query = $this->runQuery($email);
if($query->num_rows() > 0)
{
return $query->row()->cuser_name;
}
return NULL; //no user name found, send back nothing
}
下一個方法具有可被其他方法使用的代碼行。 幫助保持模型代碼干燥。 該方法返回此處記錄的CI_DB_result
實例。 注意,其他模型方法調用$this->db->select('some_column');
在調用runQuery()
之前。
protected function runQuery($email)
{
return $this->db
->from('corporate_user')
->where('email', $email)
->where('status', '1')
->get();
}
我強烈建議您修改模型方法isUserExist()
和checkCuserIs()
以返回boolean
數據類型而不是整數。 然后使用嚴格的( ===
)比較運算符。
現在到您的控制器。
更改
if ($isAlreadyRegistered > 0) {
至
if ($isAlreadyRegistered === TRUE) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.