Link to home
Start Free TrialLog in
Avatar of mmarth
mmarth

asked on

mysqli_insert_id always returns 0

mysqli_insert_id always returns 0  .    could there be a server setting that is causing this?

i have an auto incremented record , nameId, being inserted.
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Can you show us the code you're using?
Avatar of mmarth
mmarth

ASKER

function openRS($query)
{
      $prsSERVER            = $GLOBALS['theSERVER'];
      $prsUSERNAME      = $GLOBALS['theUSERNAME']      ;
      $prsPASSWORD      = $GLOBALS['thePASSWORD']      ;
      $prsDATABASE      = $GLOBALS['theDATABASE']      ;

      $connect = @mysqli_connect($prsSERVER,$prsUSERNAME,$prsPASSWORD, $prsDATABASE);
      $result  = mysqli_query($connect, $query);
      return $result;
}

$mysqliConLink = openConnect();
if ($rsHits=mysqli_query($mysqliConLink,$query))
{
      $rowcount=mysqli_num_rows($rsHits);
}

$query="INSERT INTO NamesToCheckList (parentId,LASTNAME,FIRSTNAME,BUSNAME,userId,active,facilityId,listId)
            VALUES ( '$parentId','$lastName','$firstName','$busName','1','$active','2','$listId')";
$rs=openRS($query);
$lastInsertId = mysqli_insert_id($mysqliConLink);
To set your connection you're using this:
$mysqliConLink = openConnect();

Open in new window

but the function to connect to the database has another name:
function openRS($query)

Open in new window

Avatar of mmarth

ASKER

function openRS($query)
{
      $prsSERVER            = $GLOBALS['theSERVER'];
      $prsUSERNAME      = $GLOBALS['theUSERNAME']      ;
      $prsPASSWORD      = $GLOBALS['thePASSWORD']      ;
      $prsDATABASE      = $GLOBALS['theDATABASE']      ;

      $connect = @mysqli_connect($prsSERVER,$prsUSERNAME,$prsPASSWORD, $prsDATABASE);
      $result  = mysqli_query($connect, $query);
      return $result;
}
Avatar of mmarth

ASKER

i get the same result with:

      mysqli_query($mysqliConLink, $query);
      $lastInsertId = mysqli_insert_id($mysqliConLink);

it works if i use :
$query="SELECT MAX(nameId) AS lastId FROM NamesToCheckList";
You need to check and see if the query worked.  Full instructions are in this article.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Quick summary:
1. Add error_reporting(E_ALL) to the top of all your scripts
2. Add var_dump() to print out every variable your scripts set
3. When a PHP instruction returns a value, print the value.  For examples check the Return Values shown in the PHP manual:
http://php.net/manual/en/mysqli.query.php
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmarth

ASKER

been trying to figure this out since mysql. worked with mysql_insert_id

P.S.   you did misspell mysqli_insert_id

P.S.  thanks Ray
Sorry for the mispelling... But I wrote mysqi_insert_id rather than mysqli_insert_id. So, in order to avoid to mix up mysql and mysqli functions, use mysqli_insert_id.
Thanks for points :-)