2012-04-01 50 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