资源ID#4为什么我得到这个?

noo*_*123 4 php mysql

我有一个非常基本的论坛模板,我正在为测试目的而努力

当我创建主题并按提交时,进程更新数据库但不在屏幕上输出.为什么是这样?当我从以下代码回显$ 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>
Run Code Online (Sandbox Code Playgroud)

Sib*_*ibu 10

你得到 resource id #4因为$result是一种资源,你必须通过这种方式提取其中包含的值,

$result=mysql_query($sql);
$values = mysql_fetch_array($result);
var_dump($values);
Run Code Online (Sandbox Code Playgroud)

更多关于资源变量

更新2(来自OP评论)

您正在使用字段名称打印值.在这种情况下,您必须更改为

while($rows=mysql_fetch_array($result,MYSQL_ASSOC))
Run Code Online (Sandbox Code Playgroud)

或者您可以直接使用mysql_fetch_assoc(),在您的情况下将是

while($rows=mysql_fetch_assoc($result)){
      echo $rows['id'];
}
Run Code Online (Sandbox Code Playgroud)


ujj*_*ahi 5

问题出在您的代码中:

$result=mysql_query($sql);
echo $result;
Run Code Online (Sandbox Code Playgroud)

$result是资源类型,因为mysql_query($sql)返回资源停止回显$result