+ -
当前位置:首页 → 问答吧 → 谁能解释下为什么会发生这种情况……

谁能解释下为什么会发生这种情况……

时间:2011-09-16

来源:互联网


Fatal error: Call to a member function mysql_getdata() on a non-object in C:\wamp\www\www\left.php on line 21


PHP code

<?php
/**********************
global.php
**********************/ 
    session_start();
    
    include_once('./configs/config.php');    
    include_once('./common/smarty/Smarty.class.php');    
    include_once('./common/mysql.class.php');    
//    include_once("./conn.php");
    include_once("./action.php");
    include("./common/fckeditor/fckeditor.asp");    
    
    
    $db = new action();
    $db->mysql_conn();
    
    $smarty = new smarty();
    $smarty->template_dir    = $smarty_template_dir;
    $smarty->compile_dir    = $smarty_compile_dir;
    $smarty->config_dir        = $smarty_config_dir;
    $smarty->cache_dir        = $smarty_cache_dir;
    $smarty->caching        = $smarty_caching;
    $smarty->left_delimiter = $smarty_delimiter[0];
    $smarty->right_delimiter= $smarty_delimiter[1];
    $smarty->assign("t_dir",$smarty_template_dir);    
    
    include_once("./head.php");    
?>









PHP code


<?php
/**********************
left.php

    <li>目录一
        <ul>
            <li>目录1-1</li>
            <li>目录1-2</li>
        </ul>
    </li>
    
**********************/ 
include_once("global.php");

get_cata();
$smarty->assign("title",$name);
$smarty->display("left.html");

function get_cata($level=0,$Pid=0)
{
            $result=$db->mysql_getdata("mp_cata","id=$Pid");
            while($row=@mysql_fetch_assoc($result))
            {
                    echo  $row['Cataname'];
            }
}
?>

作者: kkkgho   发布时间: 2011-09-16

函数 get_cata 中的 $db 没有被声明为全局变量

作者: xuzuning   发布时间: 2011-09-16

请问怎么声明全局变量?


global $db = new action();


这样会出错

作者: kkkgho   发布时间: 2011-09-16

get_cata()方法中 global $db 或者直接传入参数

作者: dream1206   发布时间: 2011-09-16

global $db;
$db = new action();
$db->mysql_conn();

改成这样以后声明变量处没有报错了,但是还是提示调用了一个不存在的函数,在不存在的对象下。

Fatal error: Call to a member function mysql_getdata() on a non-object in C:\wamp\www\www\left.php on line 21

作者: kkkgho   发布时间: 2011-09-16

知道。。。是什么。

作者: liangjiankang   发布时间: 2011-09-16

function get_cata($level=0,$Pid=0)
{
global $db;
$result=$db->mysql_getdata("mp_cata","id=$Pid");
while($row=@mysql_fetch_assoc($result))
{
echo $row['Cataname'];
}
}

改成这样以后就好了……但是为什么感觉不太规范呢?

作者: kkkgho   发布时间: 2011-09-16

问题出在 C:\wamp\www\www\left.php on line 21
PHP code
function get_cata($level=0,$Pid=0)
{
            global $db;
            $result=$db->mysql_getdata("mp_cata","id=$Pid");
            while($row=@mysql_fetch_assoc($result))
            {
                    echo  $row['Cataname'];
            }
}

作者: dream1206   发布时间: 2011-09-16