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

  • Home
  • SEARCH
  • 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 7728561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:44:58+00:00 2026-06-01T05:44:58+00:00

I have a function that creates a course. I am trying to get the

  • 0

I have a function that creates a course. I am trying to get the Last Insert ID but it doesn’t work:

public  function createCourse()
{
    require "/mysqli_connect.php";
    $course_q = "INSERT INTO course (....) VALUES (.....)";

    $course_r = mysqli_query($mysqli, $course_q);
    $course_n = mysqli_affected_rows($mysqli);
    if($course_n == 1)
    {
        mysqli_close($mysqli);
        return true;
    }
    mysqli_close($mysqli);
    return false;
}

This is the function to retrieve the last insert ID that I created in the same class as the function createCourse:

public function getLastInsertID()
{
    require "/../mysqli_connect.php";

    $course_q= "SELECT LAST_INSERT_ID()";

    $course_r = mysqli_query($mysqli, $course_q);
    $course_n = mysqli_num_rows($course_r);

    //var_dump($course_n);
    if($course_n)
    {
        $c = mysqli_fetch_assoc($course_r);
        mysqli_close($mysqli);

        return $c;
    }
    mysqli_close($mysqli);
    return NULL;
}   

This is how I call the functions:

require "/mysqli_connect.php";
$course = new Course();
$c = $course->createCourse();
$id = $course->getLastInsertID();
var_dump($id);

"$id" is always "int(0)"

I’ve also tried:

require "/mysqli_connect.php";
$course = new Course();
$c = $course->createCourse();
**$id = mysqli_insert_id($mysqli);**

and I’ve also tried:

$course_q= "SELECT LAST_INSERT_ID() from course";

but that doesn’t work as well. Can you guys see what the problem is? 🙁 The function createCourse itself is fine. It creates what I need and it’s there in the database but I can’t get the last insert id.

  • 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-01T05:45:00+00:00Added an answer on June 1, 2026 at 5:45 am

    Although the proper way to retrieve the insert id with MySQLi is to use mysqli_insert_id(), since you’re doing an associative fetch, you would need a column alias for the LAST_INSERT_ID()

     $course_q= "SELECT LAST_INSERT_ID() AS insert_id";
    
     // Later...
     $c = mysqli_fetch_assoc($course_r);
     echo $c['insert_id'];
    

    However, that isn’t going to work because you have already closed the connection with mysqli_close() in your createCourse() function. Instead, get the insert id inside createCourse() and return it.

    public  function createCourse()
    {
        // Note: You should not establish the connection in each function call.
        // it only needs to be done once per script, and you can pass the connection
        // into the class constructor or into methods that use it.
        require "/mysqli_connect.php";
        $course_q = "INSERT INTO course (....) VALUES (.....)";
    
        $course_r = mysqli_query($mysqli, $course_q);
        $course_n = mysqli_affected_rows($mysqli);
        if($course_n == 1)
        {
            // Get the insert id before closing the connection
            $insert_id = mysqli_insert_id($mysqli);
    
    
            // Note that there probably isn't a good reason to explicitly close the
            // connection here.  It will be closed when the script terminates.
            mysqli_close($mysqli);
    
            // And return it.
            return $insert_id;
        }
        mysqli_close($mysqli);
        return false;
    }
    
    require "/mysqli_connect.php";
    $course = new Course();
    $c = $course->createCourse();
    echo $c;
    // $c contains the id
    

    Design classes to use one common connection:

    The class receives a connection in its constructor.

    // Make classes accept the connection as a param:
    class MyClass
    {
      // property to hold the connection object
     public $db;
    
      // constructor receives the connection
      public function __construct($connection) {
        $this->db = $connection;
      }
      
      // Methods call it as $this->db
      public function createCourse() {
        $course_r = mysqli_query($this->db, $course_q);
      }
          
    }
    

    Include the connection only once on the controlling script

    require_once("/mysqli_connect.php");
    // $mysqli is now defined...
    
    // Pass it into the constructor
    $course = new MyClass($mysqli);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that creates zip files when passed an array of files
I have a function that creates a 2D vector void generate(int n) { vector<
Lets say I have a recursive function that creates lists within lists. It return
Say I have a function func(i) that creates an object for an integer i,
I have this function in a Code Igniter model that creates a new video.
I have the following function that takes a number like 5 and creates a
I have a win form that creates a site in IIS7. One function needs
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1
When you have a function with a string parameter, does that create another instance
I have used the Attach to process function within VS 2008 many times, but

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.