资源ID#4为什么我得到这个? [英] resource id #4 Why am I getting this?

查看:81
本文介绍了资源ID#4为什么我得到这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的论坛模板,用于测试目的

I have a pretty basic forum template I am working on for testing purposes

当我创建一个主题并按Submit时,该过程将更新数据库,但不会在屏幕上输出.为什么是这样?当我从下面的代码中回显$ result时,为什么得到资源ID#4:

When I create a topic, and press submit, the proccess updates the database but doesn't output on the screen. Why is this? and Why am I getting a Resource id #4 when I echo the $result from this code below:

<?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>

推荐答案

由于$result是资源,因此您正在获取resource id #4,因此必须以这种方式提取其中包含的值,

You are getting resource id #4 because $result is an resource,you must extract the values contained in it by this way,

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

有关资源变量

更新2(来自OP注释)

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

You are printing values using field name,In that case you will have to change to

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

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

Or you can directly use mysql_fetch_assoc(),which in your case will be

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

这篇关于资源ID#4为什么我得到这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆