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.
LVL 6
mmarthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Can you show us the code you're using?
0
mmarthAuthor Commented:
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);
0
Marco GasiFreelancerCommented:
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

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

mmarthAuthor Commented:
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;
}
0
mmarthAuthor Commented:
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";
0
Ray PaseurCommented:
You need to check and see if the query worked.  Full instructions are in this article.
http://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
0
Marco GasiFreelancerCommented:
Sorry for my previous comment: I was totally wrong. From your code you use two connections: one connection is the one you open within tyhe openRS function, which actually executes the query; the second one is the one you establish in the function openConnect(): this is the connection you pass to mysqli_insert_id() function.

The problem here is that mysqli_insert_id() works on a per-connection basis, so you should use the same connection used to execute the query.

One solution could be to get last insert id in openRS and make this function return an array where the first element is the result of the query and the second the last inserted id:
function openRS($query)
{
      $prsSERVER            = $GLOBALS['theSERVER'];
      $prsUSERNAME      = $GLOBALS['theUSERNAME']      ;
      $prsPASSWORD      = $GLOBALS['thePASSWORD']      ;
      $prsDATABASE      = $GLOBALS['theDATABASE']      ;

      $connect = @mysqli_connect($prsSERVER,$prsUSERNAME,$prsPASSWORD, $prsDATABASE);
      $query_result  = mysqli_query($connect, $query);
      $last_insert_id = mysqi_insert_id($connect);
      $result = array($query_result, $last_insert_id);
      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);
$query_result = $rs[0];
$last_insert_id = $rs[1];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmarthAuthor Commented:
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
0
Marco GasiFreelancerCommented:
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 :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.