免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2047 | 回复: 6
打印 上一主题 下一主题

请教一个require_once的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-01 17:13 |只看该作者 |倒序浏览
代码如下:
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

求各位大大帮帮忙,谢谢

论坛徽章:
0
2 [报告]
发表于 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);
        }

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:55:28
3 [报告]
发表于 2012-01-02 16:12 |只看该作者
class/mysql.php这里面是啥?
你把require_once改成require看看效果呢?

论坛徽章:
0
4 [报告]
发表于 2012-01-02 19:23 |只看该作者
mysql里面就是mysql的一些类,这个我在其他地方没问题的,奇怪的是换成require就没问题了,为什么啊,斑竹大大

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:55:28
5 [报告]
发表于 2012-01-02 20:35 |只看该作者
回复 4# t3st11


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

论坛徽章:
0
6 [报告]
发表于 2012-01-05 18:04 |只看该作者
你应该检查下你php的include_path下面是不是有一个没有定义这些变量的config.php

论坛徽章:
0
7 [报告]
发表于 2012-01-23 23:15 |只看该作者
这个应该是加载一次。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP