why does mysql_fetch_array() expect parameter 1 to be resource? why isn't parameter 1 resource?

后端 未结 4 1136
南笙
南笙 2021-01-26 18:09

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\\xampp\\htdocs\\test\\index.php on line 19

 

        
相关标签:
4条回答
  • 2021-01-26 18:15

    The input to mysql_fetch_Array is a resource, which is also the returned value from mysql_query. If you pass the value $sql to mysql_query(), it will not modify the parameter since it is passed by value. You have to assign the return value to another variable, which will be the desired resource.

      $result = mysql_query($sql);
    

    And then, pass the result parameter to mysql_fetch_array :

      $row = mysql_fetch_array($result, MYSQL_BOTH)
    

    Another important note: As you might see in all the related threads, read the red box in the php.net for these functions.

    Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

    mysqli_query() PDO::query()

    0 讨论(0)
  • 2021-01-26 18:30

    Cited from PHP.net about mysql_query():

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

    For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

    In your case the resource is returned, but you've forgotten to assign it to anything.

    Change mysql_query($sql) to $resource = mysql_query($sql).

    Full documentation right here: http://php.net/manual/en/function.mysql-query.php

    0 讨论(0)
  • 2021-01-26 18:34

    Change: mysql_query($sql); To: $sql = mysql_query($sql);

    0 讨论(0)
  • 2021-01-26 18:38

    Your parameter to mysql_fetch_array() function is your SQL statement string. This is what your warning say. You should first use $res = mysql_query($sql); and pass $res as parameter to mysql_fetch_array()

    0 讨论(0)
提交回复
热议问题