4

テスト目的で取り組んでいるかなり基本的なフォーラムテンプレートがあります

トピックを作成して送信を押すと、プロセスはデータベースを更新しますが、画面に出力されません。どうしてこれなの?以下のコードから$resultをエコーすると、なぜリソースID#4を取得するのですか?

<?php

$host="server"; // Host name 
$username="usernamehere"; // Mysql username 
$password=""; // Mysql password 
$db_name="forum"; // Database name 
$tbl_name="question"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending

$result=mysql_query($sql);
echo $result;
?>
<html>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>

<?php

// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>

<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td>
</tr>
</table>
</body>
</html>
4

5 に答える 5

11

resource id #4リソースであるために取得 しているので$result、この方法でそれに含まれる値を抽出する必要があります。

$result=mysql_query($sql);
$values = mysql_fetch_array($result);
var_dump($values);

リソース変数の詳細

アップデート2(OPコメントより)

フィールド名を使用して値を印刷しています。その場合は、次のように変更する必要があります。

while($rows=mysql_fetch_array($result,MYSQL_ASSOC))

または、 mysql_fetch_assoc()を直接使用することもできます。

while($rows=mysql_fetch_assoc($result)){
      echo $rows['id'];
}
于 2012-11-02T05:25:14.140 に答える
5

コードに問題があります:

$result=mysql_query($sql);
echo $result;

$resultリソースを返すため、はリソースタイプです。mysql_query($sql)エコーを停止し$resultます。

于 2012-11-02T05:30:33.437 に答える
2

リンクを確認した場合-http://php.net/manual/en/function.mysql-query.php

SELECT、SHOW、DESCRIBE、EXPLAIN、および結果セットを返すその他のステートメントの場合、mysql_query()は成功した場合はリソースを返し、エラーの場合はFALSEを返します。

したがって、リソース#4が表示されます

。何を達成したいですか?

于 2012-11-02T05:28:51.603 に答える
0
<?php

$host="server";
$username="usernamehere"; 
$password=""; 
$db_name="forum";
$tbl_name="question"; 
mysql_connect("$host", "$username", "")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending

$result=mysql_query($sql);
echo $result; //remove it
?>
<html>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>

<?php

// Start looping table row
while($rows=mysql_fetch_array($result)){

echo"<tr>
<td bgcolor=#FFFFFF>$rows['id']</td>
<td bgcolor=#FFFFFF><a href='view_topic.php?id=$rows['id']'>$rows['topic']</a><BR></td>
<td align=center bgcolor=#FFFFFF>$rows['view']</td>
<td align=center bgcolor=#FFFFFF>$rows['reply']</td>
<td align=center bgcolor=#FFFFFF>$rows['datetime'];</td>
</tr>";


// Exit looping and close connection 
}
mysql_close();
?>

<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td>
</tr>
</table>
</body>
</html>

上記のコードを確認してください私はあなたの問題が行われるべきだと思います

于 2017-08-18T11:11:15.737 に答える
-2

mysql_fetch_array()を使用する必要はありません。必要に応じて、次のようなものを試してください。

$sql="SELECT * FROM $tbl_name ORDER BY id DESC"; //that's your query
$result=mysql_query($sql);
$rows=mysql_num_rows($result);
$iteration=0;
echo "<table>";

while($iteration < $rows){
    $cell_in_your_html_table = mysql_result($result , $iteration , 'column_name_from_database');
    echo "<tr><td>".$cell_in_your_html_table."</td></tr>";
      $iteration++;
}
echo "</table>"
于 2015-04-05T23:02:40.543 に答える