Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用mysql查询创建自动递增序列号?_Php_Mysql - Fatal编程技术网

Php 如何使用mysql查询创建自动递增序列号?

Php 如何使用mysql查询创建自动递增序列号?,php,mysql,Php,Mysql,代码: MySQL集成了自动增量功能。您可以登录到phpMyAdmin并通过单击edit更改bug_id字段的属性,然后选择“A_I”字段 也可以使用SQL查询执行此操作: <?php $this->db->select('*'); $this->db->from('bugs'); $where = "project_name = '".$project."'"; $this->db->where($where);

代码:


MySQL集成了自动增量功能。您可以登录到phpMyAdmin并通过单击edit更改bug_id字段的属性,然后选择“A_I”字段

也可以使用SQL查询执行此操作:

<?php
    $this->db->select('*');
    $this->db->from('bugs');
    $where = "project_name = '".$project."'";
    $this->db->where($where);
    $sql = $this->db->get();
    $res = $sql->num_rows();
    if($res === 0)
    {
        $i = 1;
        foreach ($bug as $row) 
        {
            echo $i;
        }
    }
    else
    {
        $i = 1;
        foreach ($bug as $row) 
        {
            echo ++$i;
        }
    }
?>

$data = array(
        'project_name'=>$this->input->post('project'),
        'bug_id'=>$this->input->post('bug_id'),
        );
$query = $this->db->insert('bugs',$data);

如果@Twinfriends answer不符合您的需要(但这应该是正确的方法,您不应该重新发明方向盘),那么问题可能在于您的:

ALTER TABLE YOUR_TABLE MODIFY bug_id INTEGER NOT NULL AUTO_INCREMENT;
由于您实际上正在使用
$this->input->post('bug\u id')
作为您的新id,也许您想使用其他东西

你的$bug变量来自哪里?为了确保不会得到重复的id或错误的id,您可以在MySql查询中添加类似“ORDER BY bug_id DESC LIMIT 1”(这样您就可以将1行作为具有最高bug_id的结果),然后只使用该结果+1作为新id

   $data = array(
   'project_name'=>$this->input->post('project'),
   'bug_id'=>$this->input->post('bug_id'),
   );
   $query = $this->db->insert('bugs',$data);