php mysql_fetch_array() 期望参数 1 是资源问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2697438/
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 07:23:40  来源:igfitidea点击:

mysql_fetch_array() expects parameter 1 to be resource problem

phpmysqlhtml

提问by user225269

Possible Duplicate:
“Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given” error while trying to create a php shopping cart

可能重复:
“警告:mysql_fetch_array() 期望参数 1 是资源,给定的布尔值”尝试创建 php 购物车时出错

I don't get it, I see no mistakes in this code but there is this error, please help:
mysql_fetch_array() expects parameter 1 to be resource problem

我不明白,我看这段代码没有错误,但是有这个错误,请帮忙:
mysql_fetch_array() 期望参数 1 是资源问题

<?php

      $con = mysql_connect("localhost","root","nitoryolai123$%^");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("school", $con);
       $result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);
    ?>     


                           <?php while ($row = mysql_fetch_array($result)) { ?>             
                                     <table class="a"  border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#D3D3D3">
    <tr>

    <form name="formcheck" method="get" action="updateact.php" onsubmit="return formCheck(this);">
    <td>
    <table  border="0" cellpadding="3" cellspacing="1" bgcolor="">
    <tr>

    <td  colspan="16" height="25"  style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="2">Update Students</td>


    <tr>
    <td width="30" height="35"><font size="2">*I D Number:</td>
    <td width="30"><input  name="idnum" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers'/ value="<?php echo $_GET['id']; ?>"></td>
    </tr>

    <tr>
    <td width="30" height="35"><font size="2">*Year:</td>
    <td width="30"><input  name="yr" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers'/ value="<?php echo $row["YEAR"]; ?>"></td>

<?php } ?>

I'm just trying to load the data in the forms but I don't know why that error appears. What could possibly be the mistake in here?

我只是想在表单中加载数据,但我不知道为什么会出现该错误。这里可能是什么错误?

回答by codaddict

You are not doing error checkingafter the call to mysql_query:

调用mysql_query后,您没有进行错误检查

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);
if (!$result) { // add this check.
    die('Invalid query: ' . mysql_error());
}

In case mysql_queryfails, it returns false, a booleanvalue. When you pass this to mysql_fetch_arrayfunction (which expects a mysql result object) we get this error.

如果mysql_query失败,它返回false一个boolean值。当您将 this 传递给mysql_fetch_array函数(需要 a mysql result object)时,我们会收到此错误。

回答by Your Common Sense

$id = intval($_GET['id']);
$sql = "SELECT * FROM student WHERE IDNO=$id";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);

always do it this way and it will tell you what is wrong

总是这样做,它会告诉你什么是错的

回答by yapiyapi

Give this a try

试试这个

$indo=$_GET['id'];
$result = mysql_query("SELECT * FROM student WHERE IDNO='$indo'");

I think this works..

我认为这有效..

回答by Pascal MARTIN

You are using this :

你正在使用这个:

mysql_fetch_array($result)

To get the error you're getting, it means that $resultis not a resource.

要获得您收到的错误,这意味着这$result不是资源。


In your code, $resultis obtained this way :


在您的代码中,$result是这样获得的:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);

If the SQL query fails, $resultwill not be a resource, but a boolean -- see mysql_query.

如果 SQL 查询失败,$result将不是资源,而是布尔值——请参阅mysql_query

I suppose there's an error in your SQL query-- so it fails, mysql_queryreturns a boolean, and not a resource, and mysql_fetch_arraycannot work on that.

我想您的 SQL 查询中存在错误——所以它失败了,mysql_query返回一个布尔值,而不是一个资源,并且mysql_fetch_array无法处理它。


You should check if the SQL query returns a result or not :


您应该检查 SQL 查询是否返回结果:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);
if ($result !== false) {
    // use $result
} else {
    // an error has occured
    echo mysql_error();
    die;    // note : echoing the error message and dying 
            // is OK while developping, but not in production !
}

With that, you should get a message that indicates the error that occured while executing your query -- this should help figure out what the problem is ;-)

这样,您应该会收到一条消息,指出执行查询时发生的错误——这应该有助于找出问题所在;-)


Also, you should escape the data you're putting in your SQL query, to avoid SQL injections!


此外,您应该对放入 SQL 查询中的数据进行转义,以避免SQL 注入

For example, here, you should make sure that $_GET['id']contains nothing else than an integer, using something like this :

例如,在这里,您应该确保它只$_GET['id']包含一个整数,使用以下内容:

$result = mysql_query("SELECT * FROM student WHERE IDNO=" . intval($_GET['id']));

Or you should check this before trying to execute the query, to display a nicer error message to the user.

或者您应该在尝试执行查询之前检查这一点,以便向用户显示更好的错误消息。

回答by Sarfraz

Make sure that your query ran successfully and you got the results. You can check like this:

确保您的查询成功运行并获得了结果。你可以这样检查:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']) or die(mysql_error());


if (is_resource($result))
{
   // your while loop and fetch array function here....
}

回答by cletus

The most likely cause is an error in mysql_query(). Have you checked to make sure it worked? Output the value of $resultand mysql_error(). You may have misspelled something, selected the wrong database, have a permissions issue, etc. So:

最可能的原因是mysql_query(). 你检查过以确保它有效吗?输出的值$resultmysql_error()。你可能拼错了一些东西,选择了错误的数据库,有权限问题等。所以:

$id = (int)$_GET['id']; // this also sanitizes it
$sql = "SELECT * FROM student WHERE idno = $id";
$result = mysql_query($sql);
if (!$result) {
  die("Error running $sql: " . mysql_error());
}

Sanitizing $_GET['id']is reallyimportant. You can use mysql_real_escape_string()but casting it to an intis sufficient for integers. Basically you want to avoid SQL injection.

消毒$_GET['id']真的很重要。您可以使用,mysql_real_escape_string()但将其转换为 anint就足以用于整数。基本上你想避免 SQL 注入。

回答by zaf

In your database what is the type of "IDNO"? You may need to escape the sql here:

在您的数据库中,“IDNO”的类型是什么?您可能需要在此处对 sql 进行转义:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);