iT邦幫忙

0

SQL語法在phpmyadmin顯示的出中文但網頁顯示出來卻是問號

上網找了很多顯示問號相關的文章
卻無法找出正確答案
只好求解了
在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);
?>
ccutmis iT邦高手 9 級 ‧ 2019-02-08 19:37:47 檢舉
若確定db是unicode ,可檢查看看html碼裡面的head區段有沒有<meta charset="utf-8" /> ,還有文件編碼是不是Utf-8碼,
可參閱下列圖片:
http://www.web3d.url.tw/htmDocEncoding.jpg
scjh8214 iT邦新手 5 級 ‧ 2019-02-08 19:54:38 檢舉
有都有確認過了

2 個回答

0
小松 Nana
iT邦研究生 3 級 ‧ 2019-02-08 13:10:25
最佳解答

改成這樣連接試試

$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"
);
scjh8214 iT邦新手 5 級 ‧ 2019-02-08 17:17:56 檢舉

下面迴圈不確定是不是這樣做
執行出來是空白
不知道是不是我漏了甚麼
或是我錯了甚麼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);
?>
小松 Nana iT邦研究生 3 級 ‧ 2019-02-08 19:31:37 檢舉

喔,因為跟你之前的連接資料庫方式不太一樣~
如果這個要做迴圈
SQL語句要給個定義,就跟你上面 $sql $result 一樣
例如

$data = $mysqli->query( .... );

while 迴圈這樣就行

while ($row=mysqli_fetch_array($data)) {
//...
}
scjh8214 iT邦新手 5 級 ‧ 2019-02-08 19:56:28 檢舉

成功了 感謝!!!!

0

試一下,如果資料重要,記得先備份你的資料庫,避免資料錯誤,或是複製到新的資料庫去試
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

scjh8214 iT邦新手 5 級 ‧ 2019-02-08 17:20:11 檢舉

應該不是SERVER的問題
因為其他的網頁可以正常顯示中文

我要發表回答

立即登入回答