MYSQL将行提取到页面中

时间:2012-04-01 20:17:47

标签: php mysql sql

我从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:Fadi

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

但它不起作用:(任何提示

3 个答案:

答案 0 :(得分:1)

'BY'之后缺少一个concat运算符。应该是

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

或者更整洁:

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

答案 1 :(得分:0)

如果要返回关联数组,可以尝试使用以下行:

...

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

答案 2 :(得分:0)

您在'BY'

之后忘记了一个点
echo 'BY' . $row['editor_name'] . ' ' . $row['news_desc'] . '<br />';