草庐IT

php - fatal error : Undefined class constant

coder 2023-06-11 原文

所以我尝试使用 this mysqli 连接类(下面的代码),但我收到错误消息: fatal error :未定义的类常量 'DBUSER' [...] 我不知道为什么,因为我已经设置了所有数据库连接凭据并包含配置文件。

我的 db.config.class.php:

class config {

    public static $DBSERVER = "localhost"; // Set the IP or hostname of the database server you wish to connect to
    public static $DBNAME = "**REMOVED**"; // Set the name of the database you wish to connect to
    public static $DBUSER = "**REMOVED**"; // set the database user name you wish to use to connect to the database server
    public static $DBPASSWORD = "**REMOVED**"; // set the password for the username above
    public static $DBPORT = 3306;
    public static $TABLEPREFIX = "";

}

mysqli.class.php:

include('db.config.class.php');

/**
 * My-SQL database class
 *
 * @name        mysql
 * @version     2
 * @author      Leigh Edwards
 * @category    PHP
 */

class Dbconnect {

    // leave blank if used for multiple users and call setUser method
    private $sqlUser = "";

    // leave blank if used for multiple users and call setPassword method
    private $sqlPassword = "";

    // set this to the database name you wish to use. If this class is used to access a number of Databases
    // leave blank and call the select method to select the desired database
    private $sqlDatabase = "";

    // set this to the database server address. If you are using this class to connect to differant server
    // leave blank and call the setHost method
    private $sqlHost = "";

    // Set this to the prefix of your tables if you set one while installing.
    // default = ""
    public $table_prefix = "";
    private $result; // Query result
    private $querycount; // Total queries executed
    private $linkid;

    /////////////////////////////////////////END CONFIG OPTIONS/////////////////////////////////////////////////////


    function __construct() {

        $this->loadDefaults ();

        $this->connect ( $this->sqlHost, $this->sqlUser, $this->sqlPassword, $this->sqlDatabase );

        $this->select ( $this->sqlDatabase );


    }

    /*
     * method to load the object with the defaut settings
     */

    private function loadDefaults() {
        $this->sqlUser = config::DBUSER;
        $this->sqlPassword = config::DBPASSWORD;
        $this->sqlHost = config::DBSERVER;
        $this->sqlDatabase = config::DBNAME;
        $this->table_prefix = config::TABLEPREFIX;
    }

    public function getResult() {

        return $this->result;

    }

    /**
     * method to return the prefix for the sql tables
     *
     * @return = string $this->table_prefix
     */

    public function get_tablePrefix() {

        return $this->table_prefix;

    }

    /**
     * function to return a string from within another string
     * found between $beginning and $ending
     *
     * @param string $source
     * @param string $beginning
     * @param string $ending
     * @param string $init_pos
     */

    function get_middle($source, $beginning, $ending, $init_pos) {

        $beginning_pos = strpos ( $source, $beginning, $init_pos );

        $middle_pos = $beginning_pos + strlen ( $beginning );

        $ending_pos = strpos ( $source, $ending, $beginning_pos + 1 );

        $middle = substr ( $source, $middle_pos, $ending_pos - $middle_pos );

        return $middle;

    }

    /**
     * method to connect to the MySQL database server.
     *
     * @param   string  $sqlHost
     * @param   string  $sqlUser
     * @param   string  $sqlPassword
     **/

    function connect($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase) {
        try {
            $this->linkid = mysqli_connect ( $sqlHost, $sqlUser, $sqlPassword, $sqlDatabase, 3306 );
            if (! $this->linkid) {
                die ( 'Connect Error (' . mysqli_connect_errno () . ') ' . mysqli_connect_error () );
            }
        } catch ( Exception $e ) {
            die ( $e->getMessage () );
        }
    }

    /**
     * method to select the database to use
     * @param string $sqlDatabase
     */

    function select($sqlDatabase) {
        try {
            if (! @mysqli_select_db ( $sqlDatabase, $this->linkid )) {
                throw new Exception ( "The Selected Database Can Not Be Found On the Database Server. $sqlDatabase (E2)" );
            }
        } catch ( Exception $e ) {
            die ( $e->getMessage () );
        }
    }

    /**

     * method to query sql database
     * take mysql query string
     * returns false if no results or NULL result is returned by query
     * if query action is not expected to return results eg delete
     * returns false on sucess else returns result set
     *
     * NOTE: If you requier the the actual result set call one of the fetch methods
     *
     * @param string $query
     * @return boolian true or false
     */

    function query($query) {
        // ensure clean results
        unset ( $this->result );
        // make query
        $this->result = mysqli_query ( $query, $this->linkid );
        if (! $this->result) {
            echo "<br>Query faild: $query";
            return FALSE;
        } else {
            return true;
        }
    }

    /**
     * method to return the number of rows affected by the
     * last query exicuted
     * @return int
     */

    function affectedRows() {

        $count = mysqli_affected_rows ( $this->linkid );
        return $count;

    }

    /**
     * method to return the number of rows in the result set
     * returned by the last query
     */

    function numRows() {

        $count = @mysqli_num_rows ( $this->result );

        return $count;

    }

    /**
     * method to return the result row as an object
     * @return  object
     */

    function fetchObject() {

        $row = @mysqli_fetch_object ( $this->result );

        return $row;

    }

    /**
     * method to return the result row as an indexed array
     * @return array
     */

    function fetchRow() {

        $row = @mysqli_fetch_row ( $this->result );

        return $row;

    }

    /**
     * method to return the result row as an associative array.
     * @return array
     **/

    function fetchArray() {

        $row = @mysqli_fetch_array ( $this->result, mysqli_ASSOC );

        return $row;

    }

    /**
     * method to return total number queries executed during
     * the lifetime of this object.
     *
     * @return int
     */

    function numQueries() {

        return $this->querycount;

    }

    function setResult($resultSet) {

        $this->result = $resultSet;

    }

    /**
     * method to return the number of fields in a result set
     * @return int
     **/

    function numberFields() {

        return @mysqli_num_fields ( $this->result );

    }

    /**
     * method to return a field name given an integer offset
     * @return  string
     **/

    function fieldName($offset) {

        return @mysqli_field_name ( $this->result, $offset );

    }

    /**
     * method to return the results of the last query
     * in html table
     *
     * This method uses the $actions string to pass html code
     * this is added to the table to enable display of images or links
     * in the last columb of the table
     *
     * if boolian false is passed no html is add to the result table
     * $startCol sets the col to start displaying 0 being the first
     *
     * @param int $startCol
     * @param string or boolian false $actions
     * @return string containing html code to dispay the table
     */

    function getResultAsTable($startCol, $actions = "") {

        if ($this->numrows () > 0) {

            // Start the table
            $resultHTML = "<table width=\"80%\" border=\"0\" align=\"center\" cellpadding=\"1\" cellspacing=\"0\"><tr>";

            $resultHTML .= "<td><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr>";

            $x = $startCol;

            // Output the table header
            $fieldCount = $this->numberFields ();

            for($i = $x; $i < $fieldCount; $i ++) {

                $rowName = $this->fieldName ( $i );

                $resultHTML .= "<th align=\"left\">$rowName</th>";

            }

            if (! $actions === false) {

                $resultHTML .= "<th align=\"left\">actions</th>";

            }

            $resultHTML .= "</tr>";

            while ( $row = $this->fetchRow () ) {

                $resultHTML .= "<tr>";

                for($i = $x; $i < $fieldCount; $i ++)

                    $resultHTML .= "<td align=\"left\">" . htmlentities ( $row [$i] ) . "</td>";

                if (! $actions === false) {

                    // Replace VALUE with the correct primary key
                    $action = str_replace ( "VALUE", $row [0], $actions );

                    $resultHTML .= "<td nowrap align=\"left\">$action</td>";

                }

                $resultHTML .= "</tr>";

            }

            $resultHTML .= "</table></td></tr></table>";

        } else {

            $resultHTML = "";

        }

        return $resultHTML;

    }

    /**
     * method to retun the value of a given colum using one where clause
     * @param $table
     * @param $col
     * @param $val1
     * @param $col2
     */

    function getRow($table, $col, $val1, $col2) {

        $query = "SELECT '$col2' FROM " . $this->table_prefix . $table . " WHERE $col = '$val1'";

        $this->query ( $query );

        $resultArray = $this->fetchArray ();

        return $resultArray [$col2];

    }

    /**
     * method to test if a row conatining $x in the feild $y exists in the given $table
     * method returns true or false
     * @param $table
     * @param $col
     * @param $val
     * @return boolian
     */

    function rowExistsInDB($table, $col, $val) {
        $this->query ( "SELECT $col FROM '" . $this->table_prefix . $table . "' WHERE '$col' = '$val'" );
        if ($this->numRows () > 0) {
            return true;
        } else {
            return false;
        }
    }

    function rowExistsInDB2($table, $col, $val, $col2, $val2) {

        $query = "SELECT " . $col . " FROM " . $this->table_prefix . $table . " WHERE " . $col . " = '" . mysqli_real_escape_string ( $val ) . "' AND " . $col2 . " = '" . mysqli_real_escape_string ( $val2 ) . "'";
        $this->query ( $query );
        if ($this->numRows () > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * method to delete all rows where $col=$val in $table
     * returns int of number of affected rows or false on fail
     *
     * @param string $table
     * @param string $col
     * @param string $val
     * @return int
     */
    function deleteRow($table, $col, $val) {
        $this->query ( "DELETE FROM '" . $this->table_prefix . $table . "' WHERE '$col' = '$val'" );
        return $this->result;
    }

    // Misc methods to do some convertions and stuff


    // round or pad to 2 decimal points
    function formatNum($num, $dec = 2) {
        for($x = 0; $x <= 5; $x ++) {
            $num = sprintf ( "%01." . ($dec + $x) . "f", $num );
            return $num;
        }
    }
    /**
     * method to reverse the order of a given date
     * and fix to mysql date format
     * so DD/MM/YYYY becomes YYYY-MM-DD
     *
     * @param string $date
     * @return string
     */

    function revDate($date) {

        // first split the date string @ / int o three parts
        $dateArray = explode ( '/', $date, 3 );

        // then reorder them to YYY-MM-DD
        $revDate = array_reverse ( $dateArray );

        $i = 0;

        foreach ( $revDate as $eliment ) {

            $correctDate .= $eliment;

            if ($i < 2) {

                $correctDate .= "-";

            }

            $i ++;

        }

        return $correctDate;

    }

    /**
     * method to revers dates taken from sql database
     * so YYYY-MM-DD becomes DD/MM/YYYY
     *
     * @param string $date
     * @return string
     */

    function revSqlDate($date) {

        // first split the date string @ / int o three parts
        $dateArray = explode ( '-', $date, 3 );

        // then reorder them to DD/MM/YYYY
        $revDate = array_reverse ( $dateArray );

        $i = 0;

        foreach ( $revDate as $eliment ) {

            $correctDate .= $eliment;

            if ($i < 2) {

                $correctDate .= "/";

            }

            $i ++;

        }

        return $correctDate;

    }

}

最佳答案

写成:

config::$DBUSER;

等等

关于php - fatal error : Undefined class constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14856075/

有关php - fatal error : Undefined class constant的更多相关文章

  1. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  2. ruby-on-rails - Rails 还是 Sinatra? PHP程序员入门学习哪个好? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?

  3. ruby-on-rails - PHP 魔术方法 __call、__get 和 __set 的 Ruby 等价物 - 2

    我很确定Ruby有这些(等同于__call、__get和__set),否则find_by将如何在Rails中工作?也许有人可以举一个简单的例子来说明如何定义与find_by相同的方法?谢谢 最佳答案 简而言之你可以映射__调用带有参数的method_missing调用__设置为方法名称以'='结尾的method_missing调用__获取不带任何参数的method_missing调用__调用PHPclassMethodTest{publicfunction__call($name,$arguments){echo"Callingob

  4. ruby - Lisp - 是否适合网络编程/应用程序(交互式)? ruby 的方式是? php的方式是? - 2

    Lisp是否适合Web编程/应用程序(交互式),就像ruby​​和php一样?需要考虑的事情是:易于使用可部署性难度(尤其是对于编程初学者而言)(编辑)在阅读PaulGraham'sessay之后,我特别提到了CommonLisp.将是我的第一门编程语言。在这方面。这样做合适吗?我听说Clojure的宏功能不如CommonLisp的强大,这就是我尝试学习Clojure的原因。它教授编程并且非常强大。 最佳答案 Lisp是一个语系,而不是单一的语言。为了稍微回答您的问题,是的,存在用于各种Lisp方言的Web框架,例如用于Common

  5. 软件工程毕业设计课题(81)微信小程序毕业设计PHP校园跑腿小程序系统设计与实现 - 2

        项目背景和意义 目的:本课题主要目标是设计并能够实现一个基于微信校园跑腿小程序系统,前台用户使用小程序发布跑腿任何和接跑腿任务,后台管理使用基于PHP+MySql的B/S架构;通过后台管理跑腿的用户、查看跑腿信息和对应订单。意义:手机网络时代,大学生通过手机网购日常用品、外卖外卖、代取快递等已不再是稀奇的事情。此外,不少高校还流行着校园有偿工作,校园跑腿就成了大学生创业服务项目。        因为你在校园里,所以不会有进入的限制。并不是所有的外卖平台都可以随意进入校园,比如小黄和小蓝的双打外卖平台。许多大学禁止送餐进入学校,更不用说送餐进入宿舍了。这一措施使得校园服务市场的竞争相对不

  6. K8s部署PHP项目 - 2

    前言    前端时间PHP项目部署升级需要,需要把Laravel开发的项目部署K8s上,下面以laravel项目为例,讲解采用yaml文件方式部署项目。一、部署步骤1.创建Dockerfile文件Dockerfile是一个用来构建镜像的文本文件,在容器运行时,需要把项目文件和项目运行所必须的组件安装其中。#基础镜像FROMphp:7.4-fpm#时区ARGTZ=Asia/Shanghai#更换容器时区RUNcp"/usr/share/zoneinfo/$TZ"/etc/localtime&&echo"$TZ">/etc/timezone#替换成阿里apt-get源RUNsed-i"s@http

  7. ruby-on-rails - PHP 开发人员学习 Ruby 和 Ruby on Rails - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于StackOverflow来说是偏离主题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,describetheproblem以及迄今为止为解决该问题所做的工作。关闭9年前。Improvethisquestion我对学习Rails很感兴趣已经有一段时间了,我觉得现在正是浸入其中并实际动手实践的好时机。在过去的一周里,我阅读了所有我能找到的关于Ruby和RubyonRails的免费电子书。我刚刚读完RubyEssentials。我也一直在玩htt

  8. ruby-on-rails - Ruby 相当于 PHP 的 ucfirst() 函数 - 2

    在Ruby中(使用Rails,如果相关)将字符串首字母大写的最佳方法是什么?请注意String#capitalize不是我想要的,因为除了将字符串的首字母大写外,此函数还使所有其他字符变为小写(这是我不想要的——我想让它们保持原样):>>"aA".capitalize=>"Aa" 最佳答案 在Rails中你有String#titleize方法:"测试字符串标题化方法".titleize#=>"测试字符串标题化方法" 关于ruby-on-rails-Ruby相当于PHP的ucfirst()

  9. php - Ruby 和 PHP HMAC 不一致 - 2

    我尝试在Ruby中创建一个HMAC,然后在PHP中验证它。ruby:require'openssl'message="A522EBF2-5083-484D-99D9-AA97CE49FC6C,1234567890,/api/comic/aWh62,GET"key="3D2143BD-6F86-449F-992C-65ADC97B968B"hash=OpenSSL::HMAC.hexdigest('sha256',message,key)phashPHP:对于Ruby,我得到:20e3f261b762e8371decdf6f42a5892b530254e666508e885c708c5b

  10. php - Facebook 扼杀了公共(public) RSS 提要;如何获取带有新时间线的 Facebook 页面 RSS? - 2

    我正在尝试从Facebook提取一个页面提要到RSS,但是每次我尝试尝试时,我都会在XML中返回一个错误,内容如下:">https://www.facebook.com/profile.php?id=</a>]]>我使用的网址是:https://www.facebook.com/feeds/page.php?id=&format=rss20&access_token=我没有设置年龄限制,也没有国家/地区限制:此外,我已经尝试过使用和不使用访问token。如以下评论所述,JSONURL确实有效:https://graph.facebook.com//feed&

随机推荐