【问题标题】:mysql_fetch_array results from inside another function wont work来自另一个函数内部的 mysql_fetch_array 结果不起作用
【发布时间】:2012-06-29 12:18:13
【问题描述】:

我在这里有两个文件,在让 mysql_fetch_array 工作时遇到了一些问题。 我认为这是因为它是从另一个函数调用的?

show_results.php:

include "sql_functions.php";

function content_humanResAll (){
    q_humanResAll();
    while($row = mysql_fetch_array($q_humanResAll_result)){
            ...

sql_functions.php:

include "db_connect.php"; //connect to db

// Query for human resources
function q_humanResAll() {

    $q_humanResAll = "SELECT * FROM human_resources LIMIT 0, 30";
    $q_humanResAll_result = mysql_query($q_humanResAll) or die("could not query MySql");
    $q_humanResAll_numRows = mysql_num_rows($q_humanResAll_result);
    //return $q_humanResAll_result// tried this also, didn't work.
}

为什么我会收到错误“mysql_fetch_array() 期望参数 1 是资源,给定 null”?

顺便说一句,show_results.php 包含在 index.php 中。所以它包含很多,但这应该不是问题吧?

我也尝试过将函数 q_humanResAll() 中的变量设为全局变量,但也没有用。

如果您需要更多意见,请告诉我。

谢谢

【问题讨论】:

  • mysql_error() 会告诉你问题所在。
  • 您需要阅读变量范围:php.net/manual/en/language.variables.scope.php
  • 感谢反对票...
  • 为什么人们不赞成这个问题?不知道和不尝试(也就是无知)是有区别的。

标签: php mysql sql database


【解决方案1】:

您没有在 content_humanResAll 中定义 $q_humanResAll_result。所以你需要把它从另一个函数传回来,像这样:

function q_humanResAll() {
    $q_humanResAll = "SELECT * FROM human_resources LIMIT 0, 30";
    $q_humanResAll_result = mysql_query($q_humanResAll) or die("could not query MySql");
    $q_humanResAll_numRows = mysql_num_rows($q_humanResAll_result);
    return $q_humanResAll_result;
}


function content_humanResAll (){
    $q_humanResAll_result = q_humanResAll();
    while($row = mysql_fetch_array($q_humanResAll_result)){
            ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-25
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    相关资源
    最近更新 更多