⑴ 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"