Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:43:11+00:00 2026-06-17T12:43:11+00:00

Sorry for this beginners question and i’m not a PHP developer, but now i’m

  • 0

Sorry for this beginners question and i’m not a PHP developer, but now i’m trying to learn it.

i want to add record in MySQL data base and i’m using transactions lock.

my code is as below.

$SqlQuery="INSERT INTO tab_photo VALUES('$PhotoID','$ProjectId','$Day','$barCode','$photoName','$PhotoXml')";

$waiting = true;
while($waiting) {
    try {
        // save border data
        $stmt = $conn->prepare($SqlQuery);
        $conn->beginTransaction();
        $stmt->execute();
        sleep(1);

        $x=$conn->commit();
        echo "x value-".$x;

        echo "Success";
        $waiting = false;
    }
    catch (PDOException $e){
        echo "Failled :".$PhotoID."-".$PhotoID;
        if(stripos($e->getMessage(), 'DATABASE IS LOCKED') !== false) {
            // This should be specific to SQLite, sleep for 0.25 seconds
            // and try again.  We do have to commit the open transaction first though
            $conn->commit();
            usleep(250000);
        } else {
            $conn->rollBack();
            throw $e;
        }
    }
}

in here as output it gives,

x value-1 Success

but actually this record doesn’t add to the database.

My Questions:

  1. Even the commit is successful(output 1) how does it not added to the database?
  2. how can i check whether record is added to database? ( Is there any way to find it without write select statement?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:43:12+00:00Added an answer on June 17, 2026 at 12:43 pm

    As I understand, you expect that PDOException will be thrown when statement is failed to execute. But as I can see, exception is not thrown by default in such cases.
    See how you can change that here

    Suppose in your case you should have a code like this:

    $conn = new PDO($connection_string); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // this will force PDO to throw exception when SQL statement fails instead of simply setting an error.
    

    Suppose this will work fine for you.

    Please note that you should not use

    $SqlQuery="INSERT INTO tab_photo VALUES('$PhotoID','$ProjectId','$Day','$barCode','$photoName','$PhotoXml')";
    

    Instead of that, you should use parameters binding:

    $SqlQuery="INSERT INTO tab_photo VALUES(:PhotoID,:ProjectId,:Day,:barCode,:photoName,:PhotoXml)";
    $stmt = $conn->prepare($SqlQuery);
    $conn->beginTransaction();
    $stmt->execute(array(':PhotoID' => $PhotoID, ':ProjectId' => $ProjectId, ....));
    sleep(1);
    

    See this for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry if this question sounds kinda newbie, but I'm a beginner and want
sorry for my beginner question but i'm trying to learn RoR. In Rails 3.2
Sorry this is not a very well defined question, I am thinking about an
Sorry this is a basic question, but all my research just barely missed answering
Sorry this is probably super basic. But in all my javabean examples, I've not
sorry this is such a simple question but I can't figure it out. How
Sorry for this simple question, but I can't solve it... There is an example:
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry if this is a stupid question, but is there an easy way to
am sorry for this dumb beginner question, but i have a real problem understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.