1

I'm working on my website. To secure everything I made a new user for the database, giving it ONLY select, insert, update and delete (although not on every table).

Now, I first thought I messed something up in the permissions, but now I know I most likely didn't since even the root user doesn't return values.

This is my database class:

<?php

class database {

    private $hostname = '127.0.0.1';
    private $username = 'root';
    private $password = '************';
    private $database = 'womj';
    private $connection = null;

    public function __construct() {
        try {
            $this->connection = new PDO('mysql://' . $this->hostname . '/' . $this->database, $this->username, $this->password);
        } catch (PDOException $ex) {
            exit($ex->getMessage());
        }
    }

    public function fetchResults($query) {
        $stmt = $this->connection->prepare($query);
        $params = array_splice(func_get_args(), 1);

        $stmt->execute($params);

        return $stmt->fetchAll(PDO::FETCH_OBJ);
    }

}

I have a global file containing:

<?php

require 'db/database.php';

$database = new database();

In my index.php just for test (was testing db class and stuff) I have this:

<?php

require 'application/global.php';

$members = $database->fetchResults('SELECT * FROM members');

var_dump($members);

?>

I got 2 records in my database:

Records

So I wonder why I don't get any results. In PHPMyAdmin I get the results even through the query. The server type I'm using is MariaDB. I'm hosting everything on a Debian server.

Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63
  • Have you checked the error logs? – Jay Blanchard Mar 04 '16 at 19:21
  • I checked the apache error logs and nothing's in there. – Joshua Bakker Mar 04 '16 at 19:26
  • 3
    Data source name format isn't `mysql://`, but `'mysql:dbname=testdb;host=127.0.0.1';`, unless something changed recently. Basically, you're not connected. Also, why make a wrapper for PDO? Your class doesn't seem to shorten any code or expose any useful methods.. – N.B. Mar 04 '16 at 19:28
  • Dumb me thanks @N.B. for pointing that out. Maybe it was some other data source name format for something else I got confused with. I'm making this wrapper so I can fetch results within one line of code rather than 4/5. – Joshua Bakker Mar 04 '16 at 19:32
  • 2
    @N.B. eagle-eyed that one! If there were no errors you need to check your server setup because that would have been reported. You should add `$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` to make sure you're getting the errors from your PDO. The first `setAttribute()` line tells PDO to disable emulating prepared statements and use actual prepared statements. – Jay Blanchard Mar 04 '16 at 19:32
  • You might also want to take a look at [UTF-8 all the way through](http://stackoverflow.com/q/279170/4577762) – FirstOne Mar 04 '16 at 19:41

0 Answers0