I wrestled a bear once.
I wrestled a bear once.

Reputation: 23409

mysql_insert_id() not returning a value -

I need to retrieve the auto increment field from my database table. I tried the following but $id is always just empty.

The insert works too.

My table is as follows: idint(9) NOT NULL auto_increment,

and id is set as primary

What am I doing wrong? $conn = mysql_connect($host,$username,$password); mysql_select_db($database, $conn) or die( "Unable to select database"); include "update_activity.php"; updateActivity("logged in", "On Break");

    $date = date("m/d/y"); $starttime = time();
    $sesh = $_SESSION['fname']." ".$_SESSION['lname'];
    $q = "INSERT INTO `breaks` (date, starttime, user) VALUES ('".$date."', '".$starttime."', '".$sesh."')";

    $query = mysql_query($q, $conn);
    $id = mysql_insert_id($conn);

    echo var_dump($id); exit;

edited to show my more recent attempts

Upvotes: 1

Views: 3028

Answers (5)

thevikas
thevikas

Reputation: 1649

Have read all comments given and your replies to each. Only one of these is possible:

  1. Either the query works properly OR
  2. You are not getting the generated primary key.

Both of these can never be true.

Define, how you know query is working? Do you know the max PK before and after the running query? Is the insert happening from some other place or thread or even other user? the query is working properly from code or from your mysql client?

To diagnose the problem, we have to go though the normal way.

  1. Dump your generated query before calling mysql_query.
  2. Wrap a error checking system around your query call so php can tell you if the query worked or not. I am sure just by these two steps you will realize the root cause of the problem.

    error_reporting(E_ALL);
    ini_set('display_errors','on');
    
    echo "before calling: $q\n";
    $query = mysql_query($q, $conn);
    if(!$query)
    {
        echo "Error:" . mysql_error($conn);
        return;
    }
    echo " generated id:" . mysql_insert_id($conn);
    

Upvotes: 4

dhpratik
dhpratik

Reputation: 1138

store the values that u want to pass into an array or variable and pass it in the insert query. its should work fine then...

Upvotes: 0

dhpratik
dhpratik

Reputation: 1138

@adelphia as far as i get the idea there is a problem in the query that is executed. plz check the query properly

Upvotes: 1

GautamD31
GautamD31

Reputation: 28773

The problem with your insert query

$q = "INSERT INTO `breaks` (date, starttime, user) 
      VALUES ('".$date."', 
              '".$starttime."',
              '".$_SESSION['fname'] $_SESSION['lname']."')";

try with this

and main thing you are using most of the deprecated "mysql" things like "mysql_insert_id()"

Upvotes: 0

OmniPotens
OmniPotens

Reputation: 1125

Borrow a lead from this code extracted from here:

http://php.net/manual/en/function.mysql-insert-id.php

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>

Upvotes: 0

Related Questions