php 如何检查 id 是否已经存在 - codeigniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16125524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if id already exists - codeigniter
提问by Hashey100
i am trying to check if an id in the database already exists and if it does not then only insert that id and not the other ones that exist
我正在尝试检查数据库中的 id 是否已经存在,如果不存在,则只插入该 id 而不是其他存在的
I have tried to do a where statement that checks if their is a id exists in the database but even if their are new information it does not insert it into the database
我试图做一个 where 语句来检查它们的 id 是否存在于数据库中,但即使它们是新信息,它也不会将其插入到数据库中
Im quite lost here any guidance would be appreciated
我很迷失在这里任何指导将不胜感激
ps i dont want to update a row i want to insert a new updated one that does not exist
ps我不想更新一行我想插入一个不存在的新的更新行
$this->db->where('id',$id);
$q = $this->db->get('testing');
if($q)
{
//Do nothing
}
else
{
$this->db->set('id', $id);
$this->db->set('message', $message);
$query= $this->db->insert('testing');
}
回答by Wayne Tun
Model
模型
<?php
class Fruits_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function check()
{
$query = null; //emptying in case
$id = $_POST['id']; //getting from post value
$name = $_POST['name'];
$query = $this->db->get_where('fruits', array(//making selection
'id' => $id
));
$count = $query->num_rows(); //counting result from query
if ($count === 0) {
$data = array(
'name' => $name,
'id' => $id
);
$this->db->insert('fruits', $data);
}
}
}
?>
回答by Jonas m
$ql = $this->db->select('id')->from('testing')->where('id',$id)->get();
if( $ql->num_rows() > 0 ) {} else {
$a = array('id' => $id, 'message' => $message);
$this->db->insert('testing', $a);
}
This should do it.
这应该这样做。
回答by Marc Audet
You have a logic issue with your code that you need to fix.
您的代码存在逻辑问题需要修复。
In your code, you save the result from your query as $q = $this->db->get('testing')
,
and $q
will always evaluate to true regardless of the number of rows your return.
在您的代码中,您将查询的结果保存为$q = $this->db->get('testing')
,并且$q
无论您返回的行数如何,都将始终评估为 true。
You need to check the number of rows using $query->num_rows() > 0
and then the rest
of you code will behave as you expect.
您需要检查使用的行数,$query->num_rows() > 0
然后其余代码将按您的预期运行。
For more details, see: http://ellislab.com/codeigniter/user-guide/database/results.html
有关更多详细信息,请参阅:http: //ellislab.com/codeigniter/user-guide/database/results.html
回答by Xandervr
You need to select the id's in your MYSQL table with the id you want to check and then count the rows. If the row count is 0 then the id doesn't exist.
您需要在 MYSQL 表中选择要检查的 id 的 id,然后计算行数。如果行数为 0,则 id 不存在。
$query = mysql_query("SELECT * FROM your_table WHERE id='$id'");
$count = mysql_num_rows($query);
If($count!=0){
// id exists
} else {
// id doesn't exist
}
回答by Wayne Tun
normally 'id' field is set with auto_incrementand set primarywhich is unique and not repeatable. So there is not problem to worry about existing.
通常 'id' 字段设置为auto_increment并设置为唯一且不可重复的主要字段。所以没有担心存在的问题。
However, in your case I think you are not using it as a 'unique field'.
但是,在您的情况下,我认为您没有将其用作“唯一字段”。
Let me give you an example.
让我给你举个例子。
Here I have a table name 'fruits'
这里我有一个表名“水果”
++++++++++++++++++++++++++++++++++++
?fruit_id | int (primary)
name | text
id | int
++++++++++++++++++++++++++++++++++++++
in your model
在你的模型中
function checkId($id)
{
$query=$this->db->get_where('fruits',array('id'=>$id)); //check if 'id' field is existed or not
if($query!=null) // id found stop
{
return FALSE;
}
else // id not found continue..
{
$data = array(
'fruit_id' => $fruit_id ,
'name' => $name ,
'id' => $id
);
$this->db->insert('fruits', $data);
}
}
回答by Saddam Khan
You should try like this:
你应该这样尝试:
public function record_exists(){
$exists = $this->db->get_where('table_name', array('id' => $id));
if($exists->num_rows() > 0 ){
echo "Some message";
return false;
}else{
// Insert your data into the database...
}
}
回答by ankit suthar
For Checking An Id Or any column value exist not in database CI have a validation rule for it.
对于检查 Id 或数据库中不存在的任何列值,CI 有一个验证规则。
See it live here: Validation Rule
在这里看到它: Validation Rule
Rule: is_unique
规则: is_unique
Returns FALSE if the form element is not unique to the table and field name in the parameter. Note: This rule requires Query Builder to be enabled in order to work.
如果表单元素对于参数中的表和字段名称不是唯一的,则返回 FALSE。注意:此规则需要启用查询生成器才能工作。
Example: is_unique[table.field]
例子: is_unique[table.field]
$this->form_validation->set_rules(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);
For More Advance use of validation, You can add all validation Setting Rules Using an Array.
对于更高级的验证使用,您可以使用数组添加所有验证设置规则。
$this->form_validation->set_rules(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);
$config = array(
'your_rule_name' => array(
array(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
)
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
);
$this->form_validation->set_rules($config);