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 7090985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:07:31+00:00 2026-05-28T08:07:31+00:00

All, I’m trying the code below, and I think that as I have written

  • 0

All,

I’m trying the code below, and I think that as I have written it, the value of $inserted never becomes TRUE, I am not sure why. When I run the program, I get thousands of empty records created, and I have to manually shut down the server.

function createRecord(){ // Query creates a new empty record in table, returns the auto-incremented ID.
            $dbConnection=$this->dbConnect();
            $inserted=false;
            $IDQuery=null;
            while (!$inserted){
                $insertQuery=$dbConnection->prepare("INSERT INTO $this->table () VALUES ()");
                $inserted=$insertQuery->execute();
                if ($inserted){
                    $IDQuery=$dbConnection->prepare("SELECT LAST_INSERT_ID()");
                    $IDQuery->execute();
                }
            }
            $result=$IDQuery->fetch(PDO::FETCH_ASSOC);
            $ID=$result["Last_Insert_ID()"];
            return $ID;
        }

Many thanks in helping me with this.

Cheers,

JDelage

EDIT: I can’t recreate the problem on a stand alone piece of code, so I think the problem is not related to this part of the code.

  • 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-05-28T08:07:32+00:00Added an answer on May 28, 2026 at 8:07 am

    Instead of running a query for the last insert id you should use PDO::last_insert_id.

    http://php.net/manual/de/pdo.lastinsertid.php

    I’m not sure why you’d need to use the loop to insert your row. Try and inspect the prepared and executed statement with debugDumpParams(), you may want to use bindParam() for the prepared statement also instead of injecting the variables into the string like that.

    Edit: Did you make sure that the table you are trying to insert to has a column set for AUTO_INCREMENT? Otherwise the PDO driver will not return meaningful data for last_insert_id().

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

Sidebar

Related Questions

All, I have some script tags that are not working in Wordpress. If I
All the code/commands below are part of a PHP script that processes images from
All, I currently have two projects that are under SourceSafe that I am unable
All, See the code below: function menu() { this.menuitem=[]; this.submenu=[]; this.menuitem[0] = $('div#sivname1'); this.menuitem[1]
All, Say I have the following bit of code: select: function(start, end, allDay) {
All, I have a stored procedure on SQL Server 2005 that accepts an XML
All time am getting below error message in groovy... Could not understand whats causing
All of the Func<T> delegates return a value. What are the .NET delegates that
All I am trying to create an app that start capturing the video from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.