加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在类中加载config.php

发布时间:2020-12-13 22:19:11 所属栏目:PHP教程 来源:网络整理
导读:我想在类中加载配置文件.这是config.php的内容 ?php$__error_reporting_level=1;$server='localhost';$user='root';$password='';$dbase='eauction';? sql.php的内容 ?phpinclude ('config.php');error_reporting($__error_reporting_level);class sql{ func
我想在类中加载配置文件.这是config.php的内容

<?php
$__error_reporting_level=1;
$server='localhost';
$user='root';
$password='';
$dbase='eauction';
?>

sql.php的内容

<?php
include ('config.php');
error_reporting($__error_reporting_level);

class sql
{
 function connect()
 {
  $connections = mysql_connect($server,$user,$password) or die ('Unabale to connect to the database');
  mysql_select_db($dbase) or die ('Unable to select database!');
  return;
 }

 function login($email,$pwd)
 {
  $this->connect();
  $result = $this->qry("SELECT uid,nameF FROM user WHERE email='".$email."' AND password='".$pwd."'");
  $row=mysql_fetch_array($result);
  if (mysql_num_rows($result)>0)
   return array($row[0],$row[1]);
  else
   return array(0,0);
 }
}
?>

我使用执行代码

include ('core/sql.php');
$obj = new sql;
$data=$obj->login($email,$pwd);
print_r($data);

我收到这个错误

无法选择数据库!

忽略mysql注入问题,我只需要完美地执行代码

解决方法

可以这样做..代码上的问题必须是你在config.php中声明的变量不可访问.你可以使它全球化或尝试我的这个代码.

<?php
class ConnectionSettings {
    private $hostname = 'localhost';
    private $username = 'root';
    private $password = '';
    private $db = 'cordiac_db';
    protected $connLink;

    // protected 'connect()' method
    protected function connect(){

        // establish connection
        if(!$this->connLink = mysql_connect($this->hostname,$this->username,$this->password)) {
            throw new Exception('Error connecting to MySQL: '.mysql_error());
        }

        // select database
        if(!mysql_select_db($this->db,$this->connLink)) { 
            throw new Exception('Error selecting database: '.mysql_error());
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读