PHP(杂货店积垢)| Mysql在两个数字之间生成一个唯一的随机数
我在Mysql中用这个列创建了一个表 secretNumber主键&AI 名称 日期 等 我的想法是,当我要将一个新项目添加到此表中时,自动生成一个密码,但必须有3个要求: 唯一编号 Randon编号每次我添加一个新项目 在0-1000000之间PHP(杂货店积垢)| Mysql在两个数字之间生成一个唯一的随机数,php,mysql,codeigniter,grocery-crud,Php,Mysql,Codeigniter,Grocery Crud,我在Mysql中用这个列创建了一个表 secretNumber主键&AI 名称 日期 等 我的想法是,当我要将一个新项目添加到此表中时,自动生成一个密码,但必须有3个要求: 唯一编号 Randon编号每次我添加一个新项目 在0-1000000之间 尝试使用uniqid等功能;兰特山,;但是没有成功 您可以创建0-1000000之间的tmptable 插入到表中时,请使用from tmptable,然后必须从此表中删除该using行。 或转发查询,但这很慢,因为每次创建临时表时。若你们创造了一次诱
尝试使用uniqid等功能;兰特山,;但是没有成功 您可以创建0-1000000之间的tmptable 插入到表中时,请使用from tmptable,然后必须从此表中删除该using行。 或转发查询,但这很慢,因为每次创建临时表时。若你们创造了一次诱惑,那个么这可能是一个快速的过程 创建临时表如果不存在listtableid INT NOT NULL自动增量主键,tmp INT NOT NULL; 设置@s=CONCAT“插入列表表tmp值”,重复“1”、“1000000”、“1”; 从@s制备stmt1; 执行stmt1; 从列表表中选择id,其中id未插入按rand limit 1从表顺序中选择id; 下拉列表
您可以创建0-1000000之间的tmptable 插入到表中时,请使用from tmptable,然后必须从此表中删除该using行。 或转发查询,但这很慢,因为每次创建临时表时。若你们创造了一次诱惑,那个么这可能是一个快速的过程 创建临时表如果不存在listtableid INT NOT NULL自动增量主键,tmp INT NOT NULL; 设置@s=CONCAT“插入列表表tmp值”,重复“1”、“1000000”、“1”; 从@s制备stmt1; 执行stmt1; 从列表表中选择id,其中id未插入按rand limit 1从表顺序中选择id; 下拉列表
逻辑第一:为了实现这一点,你必须在你的crud控制器的方法中添加一个callback\u before\u insert函数。在这个回调函数中,您将创建所需的随机数,然后将其添加到$post_数组变量中,然后将$post_数组返回到控制器的方法中。在Grovery cruds的官方页面上已经有了这个例子。 因此,在控制器的某个位置添加以下内容:
function _create_unique_secret_number()
{
/*Create a random secret_Number between 0 and 1000000
* and assign it to a variable
*/
$random_unique_secret_number = mt_rand( 0, 1000000 );
/* Now make sure that your random secret number is not already "in use"
* which means that the generated id is already stored in your table.
*/
$query = $this->db->where( 'secretNumber', $random_unique_secret_number )
->get_where( 'your_table_name_goes_here' );
if( $query->num_rows() > 0 )
{
$query->free_result();
// Try again in case the randomly generated number above is in use
return $this->create_unique_secret_number();
}
$post_array['secretNumber'] = $random_unique_int;
return $post_array;
}
/* And finally call the function inside your controllers' method.
* I mean inside the method that is handling your table.
*/
$crud->callback_before_insert( array ($this, '_create_unique_secret_number') );
然后,您可以通过访问enter code here$post_array['secretNumber']值来访问杂货店crud控制器中生成的数字。逻辑首先:为了实现这一点,您必须在杂货店crud控制器的方法中添加一个callback_before_insert函数。在这个回调函数中,您将创建所需的随机数,然后将其添加到$post_数组变量中,然后将$post_数组返回到控制器的方法中。在Grovery cruds的官方页面上已经有了这个例子。 因此,在控制器的某个位置添加以下内容:
function _create_unique_secret_number()
{
/*Create a random secret_Number between 0 and 1000000
* and assign it to a variable
*/
$random_unique_secret_number = mt_rand( 0, 1000000 );
/* Now make sure that your random secret number is not already "in use"
* which means that the generated id is already stored in your table.
*/
$query = $this->db->where( 'secretNumber', $random_unique_secret_number )
->get_where( 'your_table_name_goes_here' );
if( $query->num_rows() > 0 )
{
$query->free_result();
// Try again in case the randomly generated number above is in use
return $this->create_unique_secret_number();
}
$post_array['secretNumber'] = $random_unique_int;
return $post_array;
}
/* And finally call the function inside your controllers' method.
* I mean inside the method that is handling your table.
*/
$crud->callback_before_insert( array ($this, '_create_unique_secret_number') );
然后,您可以通过访问此处的enter code$post_array['secretNumber']值来访问杂货店crud控制器中生成的数字。您的PHP版本是什么?可能重复此版本?关于此主题,已经回答了许多问题。快速搜索php唯一id会返回数百个结果。PHP版本:7.0.22是的,我试过了,但不是字母数字,我真正需要的是数字。你至少应该试着调整给出的问题答案。另外,你没有给出任何细节,你的问题非常笼统。不管怎样,我在下面给你一个答案。你的PHP版本是什么?可能是重复的。关于这个主题,已经回答了很多问题。快速搜索php唯一id会返回数百个结果。PHP版本:7.0.22是的,我试过了,但不是字母数字,我真正需要的是数字。你至少应该试着调整给出的问题答案。另外,你没有给出任何细节,你的问题非常笼统。不管怎样,我在下面给你一个答案。