⑴ php中用mysqlcount(*) 如何取返回值
PHP语言对MYSQL数据库进行COUNT的一般代码如下:
<?php
//数据库连接
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die("对不起,数据库连接失败!").mysql_errno();
}
//选择数据库
mysql_select_db("testdb");
//sql语句
$sql="SELECTCOUNT(*)AScountFROMuser";
//执行sql
$query=mysql_query($sql,$conn);
//对结果进行判断
if(mysql_num_rows($query)){
$rs=mysql_fetch_array($query);
//统计结果
$count=$rs[0];
}else{
$count=0;
}
echo$count;
?>
⑵ php SELECT count 怎么取值
首先count查询的结果只有一条
$res=$db->query('select count(*) from table');
$row=$res->fecth();
$row[0]就是count的结果
⑶ php mysql中count(id)的记录数,如何获取出来
其实用手一个个录入是最牛逼的方法
⑷ php文件怎么在数据库查询指定的某个值出现的次数,结果要纯数字的。
使用array_count_values函数可以找出数组中相同值出现的次数,array_count_values用法如下:
array_count_values — 统计数组中所有的值出现的次数
array array_count_values ( array $input)
array_count_values() 返回一个数组,该数组用 input 数组中的值作为键名,该值在:input 数组中出现的次数作为值
input:统计这个数组的值
返回值:
返回一个关联数组,用 input数组中的值作为键名,该值在数组中出现的次数作为值。
示例:
1
2
3
4
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
以上例程会输出:
1
2
3
4
5
6
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
需要取出哪个值的次数,就在返回的数组中获取对应的key值即可,例如示例中array_count_values($array)['hello']就可以取出数组中hello的个数。
⑸ php count函数使用
如果你单纯是要计算查询出的行数
用$num
=
mysql_num_rows($R1);就可以了
如果是别的,加我BAIDU
HI吧,一起讨论一下
其实你print_r($select);你就会发现你的数组是6个数据,因为mysql_fetch_array()的语法是这样的
array
mysql_fetch_array
(
resource
result
[,
int
result_type]
)
也就是说第二个参数可以取三个值MYSQL_NUM,MYSQL_ASSOC,MYSQL_BOTH(默认)
他们的意思分别是键名取数字,键名取字段名,全部
所以,你数据库选择了3个列,那$select等于就取了6个数据
(如:
[0]=>1
[id]=>1
[1]=>2
[name]=>2
[2]=>3
[password]=>3)
存入数组,如果是$select
=
mysql_fetch_array($R1,MYSQL_ASSOC),那么count($select)就是3
(如:
[id]=>1
[name]=>2
[password]=>3)
MYSQL_NUM同理
至于你说为什么只计算出6,因为取数据值需要循环,即
while($row
=
mysql_fetch_array($R1,MYSQL_ASSOC))
{
$num
+=
count($row);
}
echo
$num;
$num应该就是你想要的结果
⑹ php如何取数据库中内容
试编写代码如下:
<?php
//从数据库根据id获取颜色
functiongetColor($db,$id)
{
if($result=$db->query("SELECT*FROMcolorwhereid='".$id."'"))
{
$row=$result->fetch_assoc();
return$row['color'];
}
return'#000000';
}
$mysqli=newmysqli("localhost","test","test","room");
if($mysqli->connect_error){
printf("数据库连接错误:%s ",mysqli_connect_error());
exit();
}
?>
<tableborder="1"cellspacing="0">
<tr>
<tdbgcolor="<?phpechogetColor($mysqli,'1')?>">1</td>
</tr>
<tr>
<tdbgcolor="<?phpechogetColor($mysqli,'2')?>">2</td>
</tr>
<tr>
<tdbgcolor="<?phpechogetColor($mysqli,'3')?>">3</td>
</tr>
</table>
<?php
$mysqli->close();
?>
⑺ PHP怎么在数据库取出指定数量的值
count(*)是统计满足这个查询条件的所有记录的总数
是一个数值,你用limit 30 当然没有效果啦
因为查出来的肯定就一条记录
你可以再数据库里执行一下 select count(*) from dx_queue
假设总共有100条记录显示的结果是
count(*)
100
limit是不起作用的。。
明白么。。
⑻ php sql查询语句的count求教!
count() 了,然后再* ,就会出错了,只能写字段名了,但是出来的也不是准确的,因为 count 后,结果只有一条,出来的你要的字段也就只有一条了,
所以还是分开来写吧,先count() 再查*
$sql="select count(imageid) from interp_images where categoryid=".$c." and actived = 1"
$sql2="select * from interp_images where categoryid=".$c." and actived = 1"