php - 资源 ID #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>

最佳答案

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

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

更多关于 resource variable

更新 2(来自 OP 评论)

您正在使用字段名称打印值,在这种情况下您将不得不更改为

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

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

while($rows=mysql_fetch_assoc($result)){
      echo $rows['id'];
}

关于php - 资源 ID #4 为什么我得到这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13189764/

相关文章:

php - 如何在 CakePHP 中执行自定义查询

php - 如何在 Drupal 中保存自定义模块中的复选框值?

mysql - 将查询的一个结果除以另一个查询的结果

MySql授予用户权限

php - 使用 loggable 来引用订单行中的产品版本?

php - ISO 3166-1 alpha-2 国家/地区代码的区域名称

php - 将 $_SESSION ['somthing' ] 分配给变量时未定义索引

mysql - SQL 按元表列排序,包括没有元条目的列

具有同一个表的 2 个实例的 MySql View

mysql - 用变量模拟 row_number 忽略顺序