t3st11 发表于 2012-01-01 17:13

请教一个require_once的问题

代码如下:
index.php:

require_once ('class/mysql.php');
require_once ('class/config.php');
$db->connect( $server_name, $db_username, $db_password, $db_name, $use_pconnect );
$db->select_db($db_name);
unset( $server_name, $db_username, $db_password, $db_name, $use_pconnect );
$sql = "SELECT id FROM test where guid = '$guid'";       
$result = $db->query($sql);

config.php:

$server_name = 'localhost';
$db_username = 'root';
$db_password = '123456';
$db_name = 'testd';
$use_pconnect = 0;

老是报错

Notice: Undefined variable: server_name in
Notice: Undefined variable: db_username in
Notice: Undefined variable: db_password in
Notice: Undefined variable: db_name in
Notice: Undefined variable: db_name

Mysql error description: No database selected
Mysql error number: 1046
Date: 2012-01-01 @ 08:53

求各位大大帮帮忙,谢谢

t3st11 发表于 2012-01-01 17:26

目录应该是没问题的,就2个文件,一个index.php,然后一个class.php目录,里面放了config.php文件。

select_db定义如下:

        function select_db($dbname) {
                return mysql_select_db($dbname, $this->link);
        }

xinglu1983 发表于 2012-01-02 16:12

class/mysql.php这里面是啥?
你把require_once改成require看看效果呢?

t3st11 发表于 2012-01-02 19:23

mysql里面就是mysql的一些类,这个我在其他地方没问题的,奇怪的是换成require就没问题了,为什么啊,斑竹大大

xinglu1983 发表于 2012-01-02 20:35

回复 4# t3st11


    有种可能是在程序前面的某个函数里require_once过一次,但是变量作用域只在函数体内,你第二次require_once等于没有效果,所以造成Undefined variable。

t6760915 发表于 2012-01-05 18:04

你应该检查下你php的include_path下面是不是有一个没有定义这些变量的config.php

sychangchun 发表于 2012-01-23 23:15

这个应该是加载一次。
页: [1]
查看完整版本: 请教一个require_once的问题