上網找了很多顯示問號相關的文章
卻無法找出正確答案
只好求解了
在phpmyadmin執行語法顯示如下
mth | MealName | amount | income |
---|---|---|---|
10月份 | 鮮菇雞肉燉飯 | 2 | 150 |
但在網頁上卻顯示如下
mth | MealName | amount | income |
---|---|---|---|
10月份 | ?????? | 2 | 150 |
<?php
$con=mysqli_connect("localhost","","","");
mysqli_query("SET NAMES 'utf8'");
mysqli_query("SET CHARACTER_SET_CLIENT=utf8");
mysqli_query("SET CHARACTER_SET_RESULTS=utf8");
$sql="SELECT DISTINCTROW DATE_FORMAT(`tmealday`, '%c月份') AS mth,Menudata.MealName AS MealName, Sum(nReservedata.amount) AS amount, Sum(nReservedata.Price) AS income FROM nReservedata
INNER JOIN Menudata ON nReservedata.MealNumber=Menudata.MealNumber
GROUP BY mth,nReservedata.MealNumber ORDER BY mth ASC,income DESC";
$result=mysqli_query($con,$sql) or die('Error with SQL');
while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
$output.=$row[mth].''.$row[MealName].''.$row[amount].''.$row[income].'<br>';
}
print_r($output);
?>
改成這樣連接試試
$servername = '';
$username = '';
$password = '';
$mysqldb = '';
$mysqli = new mysqli($servername, $username, $password, $mysqldb);
if ($mysqli->connect_error) {
die(" Error: " . $mysqli->connect_error);
}
$mysqli->query("SET NAMES utf8");
$mysqli->set_charset("utf8mb4");
date_default_timezone_set('Asia/Taipei');
執行 sql 這樣用
$mysqli->query("SELECT DISTINCTROW DATE_FORMAT(`tmealday`, '%c月份') AS mth,Menudata.MealName AS MealName, Sum(nReservedata.amount) AS amount, Sum(nReservedata.Price) AS income FROM nReservedata
INNER JOIN Menudata ON nReservedata.MealNumber=Menudata.MealNumber
GROUP BY mth,nReservedata.MealNumber ORDER BY mth ASC,income DESC");
只是這樣排版似乎有點亂
記得你phpmyadmin 中文欄位是 utf8 或是 utf8mb4_general
如果是我會這樣放
$mysqli->query(
"SELECT DISTINCTROW DATE_FORMAT(`tmealday`, '%c月份')
AS mth,Menudata.MealName
AS MealName, Sum(nReservedata.amount)
AS amount,
Sum(nReservedata.Price) AS income FROM nReservedata
INNER JOIN Menudata ON nReservedata.MealNumber=Menudata.MealNumber
GROUP BY mth,nReservedata.MealNumber
ORDER BY mth ASC,income DESC"
);
下面迴圈不確定是不是這樣做
執行出來是空白
不知道是不是我漏了甚麼
或是我錯了甚麼0.0
<?php
$servername = '';
$username = '';
$password = '';
$mysqldb = '';
$mysqli = new mysqli($servername, $username, $password, $mysqldb);
if ($mysqli->connect_error) {
die(" Error: " . $mysqli->connect_error);
}
$mysqli->query("SET NAMES utf8");
$mysqli->set_charset("utf8mb4");
date_default_timezone_set('Asia/Taipei');
$mysqli->query(
"SELECT DISTINCTROW DATE_FORMAT(`tmealday`, '%c月份')
AS mth,Menudata.MealName
AS MealName, Sum(nReservedata.amount)
AS amount,
Sum(nReservedata.Price) AS income FROM nReservedata
INNER JOIN Menudata ON nReservedata.MealNumber=Menudata.MealNumber
GROUP BY mth,nReservedata.MealNumber
ORDER BY mth ASC,income DESC"
);
while($row=mysqli_fetch_array($mysqli,MYSQL_ASSOC)){
$output.=$row[mth].''.$row[MealName].''.$row[amount].''.$row[income].'<br>';
}
print_r($output);
?>
喔,因為跟你之前的連接資料庫方式不太一樣~
如果這個要做迴圈
SQL語句要給個定義,就跟你上面 $sql
$result
一樣
例如
$data = $mysqli->query( .... );
while 迴圈這樣就行
while ($row=mysqli_fetch_array($data)) {
//...
}
成功了 感謝!!!!
試一下,如果資料重要,記得先備份你的資料庫,避免資料錯誤,或是複製到新的資料庫去試
https://blog.xuite.net/smes.pc/blog/26739628-MySQL+%E4%B8%AD%E6%96%87%E4%BA%82%E7%A2%BC%E8%A7%A3%E6%B1%BA