1

I am new to PhP. I am trying to delete some rows from my sqlite database, but I can't figure out what is wrong. My code is as below:

<?php
    $app_name=array("TestApp", "MyDataBase");
    $dir = 'sqlite:/home/ravi/public_html/GcmServer/FavoriteApps.db';
    $dbh= new PDO($dir) or die("cannot open the database");
    for($i=0;$i<sizeof($app_name);$i++) {
        error_log("looop start here...............");
        error_log("FirstAppName ".$app_name[$i]);
        $result=  $dbh->Query("DELETE FROM favorite_apps WHERE appname = '$app_name[$i]'") or die( error_log("error".mysql_error() ));
        error_log("looop execute here...............");
    }
?>

And my log file is like this:

[Wed Sep 19 11:16:38 2012] [error] [client 127.0.0.1] looop start    here...............
[Wed Sep 19 11:16:38 2012] [error] [client 127.0.0.1] FirstAppName TestApp
[Wed Sep 19 11:16:38 2012] [error] [client 127.0.0.1] error

and my localhost show 1,
thanks.

1
  • 2
    mysql_error cannot see errors that happened in PDO. Use PDO::errorInfo instead. And look into prepared statements. Commented Sep 19, 2012 at 6:04

1 Answer 1

5

You are using mysql_error() function but your database is sqlite and you are using PDO, where error handling is quite different. Try to encapsulate your code into try-catch block to see more info:

<?php
  $app_name=array("TestApp", "MyDataBase");
  $dir = 'sqlite:/home/ravi/public_html/GcmServer/FavoriteApps.db';
  $dbh= new PDO($dir) or die("cannot open the database");
  try {
    for($i=0;$i<sizeof($app_name);$i++) {
      error_log("looop start here...............");
      error_log("FirstAppName ".$app_name[$i]);
      $dbh->Query("DELETE FROM favorite_apps WHERE appname = '$app_name[$i]'");
      error_log("looop execute here...............");
    }
  } catch (PDOException $e) {
    echo $e->getMessage();
  }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Some additional code is required to make PDO throw exceptions: $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.