2012-04-01 37 views
0

我正在從MySQL獲取數據到一個頁面,我不知道如果我這樣做的方式是適當的。我無法將值提取到頁面的各個部分。MYSQL獲取行到頁面

<?php 
    // Connect to database server 
    mysql_connect("localhost", "xxx", "xxx") or die (mysql_error()); 

    // Select database 
    mysql_select_db("xxx") or die(mysql_error()); 

    // SQL query 
    $strSQL = "SELECT * FROM news"; 

    // Execute the query (the recordset $rs contains the result) 
    $rs = mysql_query($strSQL); 

    // Loop the recordset $rs 
    // Each row will be made into an array ($row) using mysql_fetch_array 
    while($row = mysql_fetch_array($rs)) { 

     // Write the value of the column FirstName (which is now in the array $row) 
     echo $row['editor_name'] . " " . $row['news_desc']; 
    echo "<br />"; 
    } 

    // Close the database connection 
    mysql_close(); 
    ?> 

</div> <!-- content #end --> 

該數據庫; db

上述收益率;

news

我要做到以下幾點; BY:法迪

echo 'BY' $row['editor_name'] . " " . $row['news_desc']; 
     echo "<br />"; 

,但它不工作:(任何提示

+0

搜索 「PHP的MySQL的字符集UTF-8」 的[UTF-8一路過關斬將] – dynamic 2012-04-01 20:18:34

+0

可能重複(http://stackoverflow.com/問題/ 279170/utf-8-all-the-way-through) – dynamic 2012-04-01 20:18:53

+1

'echo'BY'$ row ['editor_name']。 「」。 $行[ 'news_desc'];回聲「
」;'應該'回聲'BY'。 $ row ['editor_name']。 「」。 $行[ 'news_desc'];回聲「
」;' – Jack 2012-04-01 20:24:55

回答

1

您在'BY'後缺少concat運算符。應該

echo 'BY' . $row['editor_name'] . " " . $row['news_desc']; 
echo "<br />"; 

或整潔:

echo "BY {$row['editor_name']} {$row['news_desc']}<br/>"; 
0

如果你想返回關聯數組,你可以嘗試使用這條線:

... 

while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) { 
... 
0

你忘了一個點'BY'

echo 'BY' . $row['editor_name'] . ' ' . $row['news_desc'] . '<br />'; 
+0

最新點 – 2012-04-01 20:29:13

+0

閱讀http://phphowto.blogspot.com.br/2006/12/concatenate-strings.html – 2012-04-01 20:30:03