Jump to content

GarrettW

Members
  • Posts

    14
  • Joined

  • Last visited

    Never

Posts posted by GarrettW

  1. How dumb - I can't edit my post now. Well, consider this a revision then. I changed the Error class a bit.

     

    core/Error.class.php

    define('E_FATAL', E_USER_ERROR);
    define('E_NONFATAL', E_USER_WARNING);
    define('E_SUGGESTION', E_USER_NOTICE);
    
    class Error {
      static $codes = array(400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error');
      static $elevel = array(E_FATAL => 'Fatal error', E_NONFATAL => 'Error', E_SUGGESTION => 'FYI');
    
      static function send ($httpcode,$severity,$text) {
        if (!headers_sent()) header("HTTP/1.0 $httpcode ".self::$codes[$httpcode]);
        echo '<b>'.self::$elevel[$severity].'</b>: '.$text;
        if ($severity == E_FATAL) die();
      }
    
      static function mysqli_connect () {
        self::send(500,E_FATAL,'Could not connect to database.<br>Error '.mysqli_connect_errno().': '.mysqli_connect_error());
      }
    }

    Didn't change the generated output.

     

    New expected output:

    Fatal error: Could not connect to database.

    Error 10061: Can't connect to MySQL server on 'localhost'

  2. Hi, I'm writing a CMS and I'm having trouble making PHP handle errors the way I want it to. The code looks fine to me, but PHP acts like it doesn't see it.

    I'm including just the necessary parts of the files in question; see if you can tell me what I'm doing wrong.

     

    index.php

    include 'core/Error.class.php';
    require 'core/DB.class.php';
      $db = DB::singleton();

     

    core/DB.class.php

    class DB
    {
      protected $mydb;
      private static $instance;
    
      private function __construct () {
        $this->mydb = new mysqli(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_NAME,MYSQL_PORT)
          or Error::mysqli_connect();
        }
      }
      static function singleton () {
        if (!isset(self::$instance)) {
          self::$instance = new self();
        } return self::$instance;
      }
    }

     

    core/Error.class.php

    define('E_FATAL', E_USER_ERROR);
    define('E_NONFATAL', E_USER_WARNING);
    define('E_SUGGESTION', E_USER_NOTICE);
    
    class Error {
      static $codes = array(400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 500 => 'Internal Server Error');
    
      static function send ($httpcode,$severity,$text) {
        if (!headers_sent()) header("HTTP/1.0 $httpcode {$codes[$httpcode]}");
        trigger_error($text,$severity);
      }
    
      static function mysqli_connect () {
        self::send(500,E_FATAL,'Could not connect to database.<br>Error '.mysqli_connect_errno().': '.mysqli_connect_error());
      }
    }

     

    Generated output:

    Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'localhost' (10061) in D:\xampp\htdocs\CwMS\core\DB.class.php on line 18

     

    Expected output:

    Could not connect to database.

    Error 10061: Can't connect to MySQL server on 'localhost'

  3. ambertch, here's my understanding of it, as it pertains to your sortable-table scenario.

     

    The Controller is where the "business logic" takes place ... i.e. all the computation and processing. The core of the application, you could say.

    The DB abstraction layer is NOT in the Controller - it is in the Model. the Model is the link between a Controller and the raw data.

    You are correct that the HTML & CSS are part of the View, but the PHP that generates the HTML is also part of the View. The View takes the Controller's output and formats it for display.

     

    someone please correct me if i'm wrong, as I'm just learning this stuff too.

  4. I thought about going that route - but I'd rather write everything from the ground up, so that (1) I know the code intimately and have complete control over it, and (2) I end up learning more in the process.

    It is quite an ambitious project that I'm working on, but it'll be good for me. :)

    Also, this is my first real encounter with OO-PHP.

  5. well, maybe that's not what i was supposed to call it. it's technically not an interface (since it's a class), but "interface" is more of the idea. I'm writing the M in MVC.

    (MVC is what I'm trying to achieve - we'll see how close I end up getting :) )

     

    and thanks for the recommendations.

  6. New member here!

     

    I've just started writing a CMS (more or less from scratch), and I'm at the point where I'm writing the database abstraction class. (Yes, I'm doing it in PHP 5 OOP.)

     

    I've done a search on here for "mysql vs mysqli" and gathered that the general opinion around here is that mysqli is the one to use. I'd agree, especially because I like the OO-ness of it -- but since what I'm writing will be used by people on all kinds of servers, I'm torn over whether I should use mysqli or the older mysql functions.

    How widespread is the mysqli extension? I have no qualms about requiring PHP 5 for my script, but if I'm correct, mysqli isn't compiled into PHP by default, so how likely would it be for any given user to have mysqli installed?

    Lastly, I've noticed that most big-name PHP packages (including Wordpress) use mysql - so again, is there a legitimate compatibility concern here?

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.