Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 资源id 4为什么我会得到这个?_Php_Mysql - Fatal编程技术网

Php 资源id 4为什么我会得到这个?

Php 资源id 4为什么我会得到这个?,php,mysql,Php,Mysql,我有一个非常基本的论坛模板,我正在进行测试 当我创建一个主题并按submit时,该过程会更新数据库,但不会在屏幕上输出。为什么会这样?当我从下面的代码中回显$result时,为什么会得到资源id 4: <?php $host="server"; // Host name $username="usernamehere"; // Mysql username $password=""; // Mysql password $db_name="forum"; // Database n

我有一个非常基本的论坛模板,我正在进行测试

当我创建一个主题并按submit时,该过程会更新数据库,但不会在屏幕上输出。为什么会这样?当我从下面的代码中回显$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>
您得到的是资源id 4,因为$result是一个资源,您必须通过这种方式提取其中包含的值

$result=mysql_query($sql);
$values = mysql_fetch_array($result);
var_dump($values);
更多关于

从OP评论更新2

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

while($rows=mysql_fetch_array($result,MYSQL_ASSOC))
或者您可以直接使用,在您的情况下

while($rows=mysql_fetch_assoc($result)){
      echo $rows['id'];
}
如果您检查链接-

对于返回resultset的SELECT、SHOW、description、EXPLAIN和其他语句,mysql_query在成功时返回资源,在错误时返回FALSE

因此,您看到的是资源4


。您想要实现什么?

问题在于您的代码:

$result=mysql_query($sql);
echo $result;
$result是资源类型,因为mysql\u query$sql返回资源
停止回显$result。

您不必使用mysql\u fetch\u数组。如果需要,请尝试以下方法:

$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>"

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

,因为$result就是一个资源。只需删除echo$results while循环不应该处理资源4错误吗?如果我只是删除echo$result..我的数据仍然没有显示在表中..我只是尝试在上面的代码中将数据库中的数据输出到表中OK,我的数据显示在var_dump中,但在上面的代码中它不会显示在表中。如果信息被传递,为什么会出现这种情况?你是否在服务器上启用了短标记,替换@noob123我的错误,我应该早点看到,检查我的更新答案我在上面的代码中使用了mysql\u fetch\u assoc,但它不起作用..我不知道我做错了什么..添加命令mysql\u fetch\u assoc$result对我有效,谢谢!欢迎来到堆栈溢出。请在答案中为您的代码段使用代码块。请仔细检查您的答案,您的$num变量显然为空,这使您的代码毫无用处。请为变量tada使用相关且可读的名称?这是魔术的结果吗?。请确保您确实回答了OP问题。
<?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>