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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:21:05+00:00 2026-05-28T03:21:05+00:00

Apologies for the vague title, here’s my problem: The goal of my code is

  • 0

Apologies for the vague title, here’s my problem:

The goal of my code is to insert a new row into a table that has an auto-increment field. After the insert, I want to get the value of the auto-increment field that has just been generated.

Here’s my table defintion:

CREATE TABLE `EventComments` (
  `CommentID` int(11) NOT NULL AUTO_INCREMENT,
  `EventID` int(11) NOT NULL,
  `OwnerID` int(11) NOT NULL,
  `Comment` varchar(512) NOT NULL,
  `DateTime` datetime NOT NULL,
  PRIMARY KEY (`CommentID`)
) ENGINE=MyISAM AUTO_INCREMENT=68 DEFAULT CHARSET=latin1;

I’m trying to get the value of the CommentID field.

So, here is the php code that issues the insert query and then attempts to get the CommentID value.

<?php
session_start();
ob_start();
include_once 'lib/functions.php';

if(isset($_SESSION['uid'])) {
    $eventID = $_GET['evid'];
    $ownerID = $_SESSION['uid'];
    $comment = $_GET['comment'];
    $comment = trim($comment);
    $dateTime = date('Y-m-d H:i:s');

    $db_connection = database_connect();

    if($eventID != null && !empty($comment)) {
        $query = "INSERT INTO meetup.EventComments (EventID, OwnerID, Comment,     DateTime) 
        VALUES (" . $eventID . ", " . $ownerID .", '" . $comment . "', '". $dateTime ."')";

        mysqli_query($db_connection, $query) or die(mysqli_error($db_connection));  

        $id = mysql_insert_id();
        $commentHtml = generateCommentFromData($db_connection, $ownerID, $comment, $dateTime,  $id);
        echo $commentHtml;
    }
}

ob_end_flush();
?>

This code issues the following error in the php logs:

mysql_insert_id() [<a href='function.mysql-insert-id'>function.mysql-insert-id</a>]: A link to the server could not be established...

I also tried explicitly passing the database link. But that gives the following error:

mysql_insert_id(): supplied argument is not a valid MySQL-Link resource...

As a final note, the insert query works. It is definitely inserting a new row with the expected data!

Any insight here would be appreciated!
Thanks

  • 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-28T03:21:06+00:00Added an answer on May 28, 2026 at 3:21 am

    You are using the mysqli extension to connect and run your query, but then you use the mysql (notice the lack of i at the end) extension to get your inserted id, that can’t work. While they are both extensions that provide access to mysql, they are also two very different libraries and can’t share a connection between each other.

    For the record, mysqli is the one you should be using, mysql is the “old” version that does not support new features of mysql >= 4.1

    In other words, the solution is to use mysqli_insert_id()

    Also, please escape your parameters properly, you can’t put the content of $_GET and $_POST variables inside your query unsecured like that. At the very least use mysqli_real_escape_string()

     $query = "INSERT INTO meetup.EventComments (EventID, OwnerID, Comment,     DateTime) 
            VALUES (" . mysqli_real_escape_string($eventID)." [...]
    

    For more infos on this, have a look to the numerous questions on this subject, for example this one: How to properly escape a string via PHP and mysql

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

Sidebar

Related Questions

Apologies for the vague title, but this is quite a difficult problem to explain
Apologies for the vague question. Here it is: I have a table object created
Apologies for the somewhat vague title, I'll try explain more here. Currently, I have
I understand that the title was very vague and my apologies. I am using
Firstly, apologies for the vague title, but I'm not sure exactly what I'm asking
I apologize for the vague title. I am attempting to write a query that
Apologies for the appalling title. I have mocked up this code to mimic an
Confusing title...my apologies. What I've got is a table with two related rows. I
apologies for vague question title but not sure on correct wording, please correct if
Apologies if the title is vague, but it's not the easiest question to describe.

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.