2010-03-09 34 views
0

我創建了以下腳本來查詢表並返回前30個結果。查詢返回30個結果,但它們沒有任何文本或信息。爲什麼會這樣?爲什麼我在使用php查詢mysql時返回空記錄?

該表存儲越南文字符。數據庫是mysql4。

這裏的頁面:http://saomaidanang.com/recentposts.php

下面的代碼:

<?php 
    header('Content-Type: text/html; charset=utf-8'); 
//CONNECTION INFO 
$dbms = 'mysql'; 
$dbhost = 'xxxxx'; 
$dbname = 'xxxxxxx'; 
$dbuser = 'xxxxxxx'; 
$dbpasswd = 'xxxxxxxxxxxx';  
$conn = mysql_connect($dbhost, $dbuser, $dbpasswd) or die('Error connecting to mysql'); 
mysql_select_db($dbname , $conn); 

//QUERY 
$result = mysql_query("SET NAMES utf8"); 
$cmd = 'SELECT * FROM `phpbb_posts_text` ORDER BY `phpbb_posts_text`.`post_subject` DESC LIMIT 0, 30 '; 
$result = mysql_query($cmd); 
?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html dir="ltr"> 
<head> 
<title>recent posts</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 
<body> 
<p> 

<?php 
//DISPLAY 
while ($myrow = mysql_fetch_row($result)) 
{ 
    echo 'post subject:'; 
    echo(utf8_encode($myrow ['post_subject'])); 
    echo 'post text:'; 
    echo(utf8_encode($myrow ['post_text'])); 
} 
?> 
</p> 
</body> 
+0

爲什麼要使用函數utf8_encode?這似乎很有必要 – 2010-03-09 06:46:39

+0

@Col。 Shrapnel:因爲我不知道我在做什麼? 說真的,我使用它,因爲這個stackoverflow問題。 http://stackoverflow.com/questions/1198701他有一個類似的問題,utf8_encode解決了它。 – 2010-03-09 13:43:48

+1

正如我所見,他說SET NAMES utf8做了詭計。 嘗試你的代碼沒有utf8_encode,它應該工作正常。 你已經設置好了一切,不需要額外的重新編碼。 – 2010-03-09 14:09:00

回答

2

嘗試使用的mysql_fetch_assoc()代替mysql_fetch_row()

  • 前者返回一個陣列,存儲陣列中的每個 結果列 偏移量,從偏移量0開始。
  • 以後會返回一個關聯 字符串數組,其中 列用作鍵。

而在你的程序中,您正在使用的結果作爲關聯數組:

$myrow ['post_subject'] 
+0

這看起來很有希望。我今晚會回家時試試這個。 – 2010-03-09 14:21:39

+0

@codaddict,你是一個天才。謝謝。 – 2010-03-10 01:24:53

1

或者你可以嘗試mysql_fetch_object()和訪問數據爲$myrow->post_subject。如果您覺得數據仍未顯示,請對輸出執行print_r($myrow)。這樣你就可以確定數據是否已經被返回。

0

如果您使用mysql_fetch_row(),您將需要通過編號鍵(即$ row [0],$ row [1]等)訪問您的數據。如Shamly所述,mysql_fetch_object()在大多數應用程序中都是很常見的。

我不喜歡使用PHP提供的功能來做日常編碼。相反,考慮編寫(或下載)類或幾個函數來幫助你做的工作,比如:

function my_query($query){ 
    $return = array(); // stuff to return 

    $result = mysql_query($query); 
    while($row = mysql_fetch_object($result)){ 
    array_push($return, $row); 
    } 

    return $return; 
} 

那麼接下來你需要做的就是這個在你的腳本:

// connect to db, fill in the appropriate arguments... 
$link = mysql_connect(...); 

// selecting database 
mysql_select_db($dbname, $link); 

// do the query 
$rows = my_query(...); 
foreach($rows as $row){ 
    print_r($row); // see what's in there... 
} 

通過使用my_query()函數,您可以節省幾個步驟。這對循環內的嵌套查詢也特別有用。什麼更容易閱讀?

// assume database is already connected 
$result = mysql_query('SELECT user_id FROM table'); 
while($row = mysql_fetch_object($result)){ 
    $result_2 = mysql_query('SELECT * FROM other_table WHERE user_id = '.$row->user_id); 
    while($row_2 = mysql_fetch_object($result_2)){ 
     // do stuff with both rows 
     // potential for lots of confusion and mysql errors 
    } 
} 

或本...

// assume database is already connected 
$user_ids = my_query('SELECT user_id FROM table'); 
foreach($user_ids as $x){ 
    $more_data = my_query('SELECT * FROM other_table WHERE user_id = '.$x->user_id); 
    foreach($more_data as $y){ 
     // do stuff 
    } 
} 

反正,大概更長的回答比你預期的,但我希望它給你如何去了解的東西:)如果你有時間的想法,安裝Wordpress並學習如何使用其內置的$ wpdb對象。您將獲得關於在實際應用中如何使用數據庫的豐富知識,並且您將能夠創建自己的數據庫類以滿足您的需求。

免責聲明:我只是爲這篇文章寫了所有上述代碼,沒有測試語法錯誤。我很抱歉,如果有的話。

0

使用:

while ($myrow = mysql_fetch_array($result)) 
{ ... } 

,將工作

相關問題