TAGS :Viewed: 6 - Published at: a few seconds ago

[ My openshift application PDO query didn't work ]

I am try to connect phpmyadmin database using my php script in openshift

but the result is a empty page.

then, I find the question is the query didn't work

but I don't know why

There is my original code

try{
        $dsn = 'mysql:dbname=exampleDataBase;host=127.**.***.***;port=*****';
        $dbh = new PDO($dsn, "account", "password");
        $sth = $dbh->prepare('SELECT * FROM test1');
        $fin = $sth->execute();
        while($row = $sth->fetch(PDO::FETCH_ASSOC)){
            print_r($row);
        }
    } catch (PDOException $e){
        echo "Sytan error" . $e -> getMessage();
    }
    $dbh = null;

and the result is a empty page, so I modify my code

There is my modify code

try{
        $dsn = 'mysql:dbname=exampleDataBase;host=127.**.***.***;port=*****';
        $dbh = new PDO($dsn, "account", "password");
        $sth = $dbh->prepare('jngfcjfgcnmgcm,,hmnxf');
        $fin = $sth->execute();
        while($row = $sth->fetch(PDO::FETCH_ASSOC)){
            print_r($row);
        }
    } catch (PDOException $e){
        echo "Sytan error" . $e -> getMessage();
    }
    $dbh = null;

I input the wrong query sytanx(jngfcjfgcnmgcm,,hmnxf), but it didn't return error.

Answer 1


You modified your code to a wrong statement to see the error message? You have your PHP errors turned off, when doing a statement like:

$sth = $dbh->prepare('jngfcjfgcnmgcm,,hmnxf');

You would receive an error like:

Sytan errorSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'jngfcjfgcnmgcm,,hmnxf' at line 1

What do you exactly want? The exception is not showing?

Answer 2


Add this to your script see your errors

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors',1);
ini_set('html_errors', 1);

and change your query code to this, see notes

  try{
    //port=***** is only need where its different from the default
    $dsn = 'mysql:host=localhost;dbname=exampleDataBase';
    $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
    $dbh = new PDO($dsn, "account", "password", $options);
    $sth = $dbh->prepare('SELECT * FROM test1');
    // execute $sth
    $sth->execute();
    //Change fetch to fetchAll
    while($row = $sth->fetchAll(PDO::FETCH_ASSOC)){
        print_r($row);
    }
} catch (PDOException $e){
    echo "Sytan error" . $e->getMessage();
}