Home > Backend Development > PHP Tutorial > PHP 数据库解决方法

PHP 数据库解决方法

WBOY
Release: 2016-06-13 11:52:45
Original
829 people have browsed it

PHP 数据库
数组:Array ( [0] => Array ( [id] => 42 ) [1] => Array ( [id] => 49 ) [2] => Array ( [id] => 50 ) [3] => Array ( [id] => 51 ) [4] => Array ( [id] => 52 ) ) ?,这里为数据表里的id字段,怎样根据这些id,修改每一个id对应的另一个字段count?第一个id的count加1,第二个id对应的加2,第三个加3.。。。。这样循环下去
------解决方案--------------------

$arr = array ( 0 => array ( 'id' => 42 ) ,1 => array ( 'id' => 49 ), 2 => array ( 'id' => 50 ), 3 => array ( 'id' => 51 ), 4 => array ( 'id' => 52 ) );<br />foreach ($arr as $key => $value) {<br />	$sql = "update tableName set count = count + ".($key+1)." where id = ".$value['id'];<br />	mysql_query($sql);<br />}
Copy after login

------解决方案--------------------
$ar = array ( 0 => array ( 'id' => 42 ) ,1 => array ( 'id' => 49 ), 2 => array ( 'id' => 50 ), 3 => array ( 'id' => 51 ), 4 => array ( 'id' => 52 ) );<br />$s = join(',', array_map('current', $ar));<br />$sql = "update tbl_name set count=count+find_inset(id,'$s')";<br />mysql_query($sql);<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template