0

I tried to simplify this as much as possible just to understand what's happening but it's still not working. It doesn't show any errors, but nothing gets added to database.

The bootstrap file:

class Connection {
    public static function make($config){
        return new PDO(
            $config['connection'].';dbname='.$config['name'],
            $config['username'], $config['password'], $config['options']
        );  
    }
}

class QueryBuilder {
    protected $pdo;

    public function __construct($pdo){
        $this->pdo = $pdo;
    }

    public function insert($table, $paramater, $value){     
        $statement = $this->pdo->prepare("insert into $table ($paramater) values($value)");
        $statement->execute();
    }
}

return new QueryBuilder(Connection::make($config['database']));

Other file after post request:

$query = require 'bootstrap.php';

$query->test('todos', 'description', 'lorem ipsum');

My $config array is this

return [
  'database' => [
    'name' => 'mytodo', 
    'username' => 'root', 
    'password' => '', 
    'connection' => 'mysql:host=ip', 
    'options' => [ ] 
  ]
]; 
Phil
  • 157,677
  • 23
  • 242
  • 245
Magdi Gamal
  • 681
  • 1
  • 9
  • 25
  • What do the values in `$config` look like, particularly `connection` and `options` (you can omit username and password)? – Phil May 30 '18 at 03:50
  • I don't think ti's the connection, cause when I try selecting * it works just fine... $statement = $this->pdo->prepare("select * from $table"); $statement->execute(); – Magdi Gamal May 30 '18 at 03:54
  • Make sure your dev environment is set to show all errors. This is best done in the `php.ini` file with `display_errors = On` and `error_reporting -= E_ALL`. You should also set PDO to throw exceptions by adding `PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION` to `$config['options']` – Phil May 30 '18 at 03:54
  • Quite simply, you are not quoting your string values in your queries. This question has been asked and answered before – Phil May 30 '18 at 03:59

1 Answers1

0

According to the manual at http://php.net/manual/en/pdo.connections.php, your connection string is missing "mysql:".

Try:

return new PDO("mysql:".$config['connection'] etc.

Also, try putting a try-catch block around it and display the message from whatever Exception it may be throwing.

UncaAlby
  • 5,146
  • 1
  • 16
  • 19
  • How do you know what's in OP's `$config` array? – Phil May 30 '18 at 03:49
  • Hey thanks for responding. I added "mysql:". but it's still not working :( I'll try a try-catch statement then get back to you. – Magdi Gamal May 30 '18 at 03:51
  • @Phil, I don't, and it may well be equal to "mysql:". However, the try-catch block still ought to render useful info. – UncaAlby May 30 '18 at 03:51
  • my $config array is this return [ 'database' => [ 'name' => 'mytodo', 'username' => 'root', 'password' => '', 'connection' => 'mysql:host=ip', 'options' => [ ] ] ]; – Magdi Gamal May 30 '18 at 03:52
  • @UncaAlby a `try..catch` block will only help hide any errors. It's hard to miss uncaught exceptions – Phil May 30 '18 at 03:52
  • @Phil, I disagree. PHP can sometimes be terribly quiet about some errors some of the time, depending on how it's configured. I would suggest to the OP to start by copying the code in the manual at the link I provided. It can't get any simpler than that. Bottom line, there are a gazillion things that might be wrong, from misspelling the credentials, to trying to connect to "localhost" from "::1" (yah, they're supposed to be the same, right? No.) to the Mysql service not even running. Displaying the error message in the catch block should be informative. – UncaAlby May 30 '18 at 03:56
  • The best thing to do (while developing) is make sure all errors are displayed. I would then advise not using any exception handling (during initial development) as an uncaught exception will terminate the process and be reported in full. – Phil May 30 '18 at 03:57
  • I almost agree with that, Phil, except that sometimes in the rush to get things out the door, developers neglect to go back and add the appropriate try-catch blocks before it goes out to production. So guess what happens? Something goes "URK" during production, the whole thing comes to a screeching halt, and customers are left wondering what the heck happened! I'm in favor of having try-catch blocks in place from the start, with some mechanism in the catch block to quickly alert developers to problems, no matter the current stage. Otherwise, yes, you're absolutely right. – UncaAlby May 30 '18 at 04:07