php循环生成10个唯一代码,但只保存mysql上的最后一个代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php循环生成10个唯一代码,但只保存mysql上的最后一个代码相关的知识,希望对你有一定的参考价值。
我已尝试多次使用下面的代码将10个独特的php生成代码保存到列中,并且我使用最后生成的代码更新了列。这段代码有什么问题?
//GNRT_id
for($c=0;$c<10;$c++)
{
$d_gnrt_id = openssl_random_pseudo_bytes(20);
$hex = bin2hex($d_gnrt_id);
//add GNRT CDE to data
$q ="UPDATE mstfa_data SET d_gnrt_id = '$hex' where x_id > 1 ";
$res = mysql_query($q);
echo "Done No: ".$c." AND CODE IS: ".$hex." and SQL_ERR=: ".mysql_error()."<br />";
}
php结果是这样的(没有错误)
Done No: 0 AND CODE IS: e899bff4566b49097f06916a14a31ce7c6194f50 and SQL_ERR=:
Done No: 1 AND CODE IS: 083edbe7bfca8186362911d1befad39694b17074 and SQL_ERR=:
Done No: 2 AND CODE IS: 1f612c052b5116dda2178d69d8e557c76303ab38 and SQL_ERR=:
Done No: 3 AND CODE IS: 51c33f7b74e6b279f123738054aba47fcc1666a2 and SQL_ERR=:
Done No: 4 AND CODE IS: d77d7b5321254d1f3c1ac401c56ff85445a6b6b0 and SQL_ERR=:
Done No: 5 AND CODE IS: 8e7e0cf691229e240c5ed71a9df28a8718ca7d04 and SQL_ERR=:
Done No: 6 AND CODE IS: 9c6c5dd99652ac3532d24480ed63f7317a93455d and SQL_ERR=:
Done No: 7 AND CODE IS: b0d9559afad928042885267a3d5954f3a4c459b0 and SQL_ERR=:
Done No: 8 AND CODE IS: a56096c3e6dd9fd6a75196b3b8236a593590a228 and SQL_ERR=:
Done No: 9 AND CODE IS: a185b07969d859912f4f0a18c25dea9db97c1dc2 and SQL_ERR=:
和所选列中的MYSQL结果是这样的
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
a185b07969d859912f4f0a18c25dea9db97c1dc2
我该怎么办?
答案
MySql条件在逻辑上是错误的,它用(id> 1)更新所有行,这意味着更新所有行*循环时间(10次)所以我修复了这个代码就像这样
$q = "select * from $tbl_1 where x_id >0";
$c_num = mysql_num_rows(mysql_query($q));
for($c=1;$c<$c_num;$c++)
{
//GNRT_id
$d_gnrt_id = openssl_random_pseudo_bytes(20);
$hex = bin2hex($d_gnrt_id);
//add GNRT CDE to data
$q ="UPDATE mstfa_data SET d_gnrt_id = '$hex' where x_id = $c ";
$res = mysql_query($q);
echo "Done No: ".$c." AND CODE IS: ".$hex." and SQL_ERR=: ".mysql_error()."<br />";
}
以上是关于php循环生成10个唯一代码,但只保存mysql上的最后一个代码的主要内容,如果未能解决你的问题,请参考以下文章