iT邦幫忙

0

distinct在php上的輸出錯誤

我想要製作一個測試使用者目前在網站上登入幾次的系統,以天次計算
資料表大概是這樣子的

資料表 : logincounter
LCID logindate
LC002 2017-05-16
LC002 2017-05-15
LC002 2017-05-15
LC002 2017-05-15
LC002 2017-05-12
LC002 2017-05-12
LC001 2017-05-12
LC001 2017-05-12
LC001 2017-05-12

原本使用"select distinct logindate from logincounter where LCID='LC0002'"
這個sql語法應該是能夠抓到3筆資料的

logindate
2017-05-16
2017-05-15
2017-05-12

但是我在php的網頁上抓取陣列卻只看到一筆資料,不曉得問題出在哪裡

程式碼 :

<?php
session_start();
header("Content-Type: text/html;charset=utf-8"); 

$db_host='localhost';
$db_user='root';
$db_password='a123456s';
$db_name='demo2';

try{
		//連線資料庫
		$db_link = new PDO("mysql:host={$db_host};dbname={$db_name};charset=utf8", $db_user, $db_password);
	} catch (PDOException $e) {
	    print "資料庫連結失敗,訊息:{$e->getMessage()}<br/>";
    	die();
    }


$sql="select distinct  logindate from logincounter where LCID='LC0002'";
$stmt=$db_link->query($sql);
	$row=$stmt->fetch();
	print_r($row);
?>

輸出成果:
Array ( [logindate] => 2017-05-12 [0] => 2017-05-12 )

wonton iT邦高手 6 級 ‧ 2017-05-15 11:08:49 檢舉
因為你只有 fetch 一次當然只有一筆資料。
while ($row=$stmt->fetch()) print_r($row);
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答