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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13189764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:56:13  来源:igfitidea点击:

resource id #4 Why am I getting this?

phpmysql

提问by noob123

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

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

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:

当我创建一个主题并按下提交时,过程会更新数据库但不会在屏幕上输出。为什么是这样?当我从下面的代码中回显 $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>

回答by Sibu

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

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

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

More about resource variable

更多关于资源变量

Update 2(From OP comments)

更新 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))

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

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

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

回答by ujjwalwahi

Problem is in your code:

问题出在您的代码中:

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

$resultis resource type, since mysql_query($sql)returns resource Stop echoing $result.

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

回答by Joddy

If you check the link - http://php.net/manual/en/function.mysql-query.php

如果您检查链接 - http://php.net/manual/en/function.mysql-query.php

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error

对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 成功时返回资源,错误时返回 FALSE

Hence you are seeing the Resource#4

因此你看到的是资源#4

. What is it you want to achieve?

. 你想达到什么目的?

回答by Mustaqeem Khan

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

just check above code i think your problem should be done

只需检查上面的代码,我认为您的问题应该解决

回答by oxy_moron

You don't have to use mysql_fetch_array(). If you want, try something like this:

您不必使用 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>"