print_r an insert statement php pdo

1.4k Views Asked by At

I'm using this method to insert data into my database:

function insertMenue($content, $date) {
    $session = $_SESSION['aid'];
    global $pdo;
    $pdo->exec('SET CHARACTER SET utf8');
    $query = $pdo->prepare('INSERT INTO menue(type, content, date, creator) VALUE (?,?,?,?)');
    $query->bindValue(1, "menue");
    $query->bindValue(2, "<p>" . $content . "</p>");
    $query->bindValue(3, $date);
    $query->bindValue(4, $session);
    $query->execute(); 
}

I'm calling this method for every object in an array. Now every time when there should be a String which contains an umlaut (ä, ö, ü) the String gets cut off where the umlaut should be.

As for example I'm writing:

<p>Salat<br>Gemüse und Teigwaren</p>

The data in the database happens to be just:

<p>Salat<br>Gem

Now the question is:

  1. How can I print_r() the whole sql statement?

    print_r($query->execute());

Would display (1,1,1,1)

and I want something like: (menue, (p)Salat(br)Gemüse und Teigwaren(/p), 2015-09-06, 2)

I'm not sure whether it doesn't get to the database or whether the database is the problem. The db itself can handle umlaute and is written in utf-8. I dumped the file and took a closer look at it, it shouldn't be corrupted.

2

There are 2 best solutions below

0
On BEST ANSWER

You cant see the full query, because it doesnt exist in the PHP side.

PHP first sends the query and then the parameters when using prepared statements (Not sure what happens when the emulation mode is on though).

If you want to see the final query, you should enable your database's query log and check there.

This is the closest you can get with just PHP: https://www.php.net/manual/en/pdostatement.debugdumpparams.php

1
On

Try this...

function insertMenue($content, $date) {

    $session = $_SESSION['aid'];

    global $pdo;
    $pdo->exec('SET CHARACTER SET utf8');

    $params = ["menue", "<p>$content</p>", $date, $session];

    // Check all your params are set...
    // Although you may want to consider checking these before entering this block
    print_r($params);

    $sql = "INSERT INTO menue( type
                             , content
                             , date
                             , creator
                             )
            VALUES( ?
                  , ?
                  , ?
                  , ?
                  )";
    try {
        $sth = $pdo->prepare($sql);
        $sth->execute($params); 
    } catch (PDOException $e) {
        throw new pdoDbException($e);
    }

}