mysql_insert_id() gives a 0 return

Posted on 2011-10-05
Last Modified: 2012-05-12

I am trying to get an auto increment ID for a user from a DB after inserting it with mysql_insert_id() but it keeps giving me a zero result.

This is the code:

	$query = "INSERT INTO tbl_users (name, middlename, surname, id, email, cell_no, password) VALUES ('".$this->name."',
				'".$this->middlename."', '".$this->surname."', '".$this->id."','".$this->email."', '".$this->cellNumber."', '')";
				$result = mysqli_query($dbc, $query) or die("Could not exectute query");
				$idResult = mysql_insert_id();
				echo $idResult;

Open in new window

$idResult give me a "0"

in the Db the users_id field is a primary key and auto increment.
Question by:QuintusSmit
    LVL 82

    Accepted Solution

    You're using the wrong function.  Should be mysqli_stmt_insert_id().  You can't use any 'mysql' functions mixed with 'mysqli' functions.  They are two different drivers and they wont' know anything about each other.  Please read the notes and comments for another function to do this.
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    Just adding a comment that might help you debug in the future.  When I'm having trouble with mySQL, I use echo to show me the query command my program constructed.  If the problem isn't obvious (and it often is), I open command line mySQL and copy/paste the command to it so that I can see an error message coming from mySQL.  If the error is still not obvious, you have something pretty useful to post to Experts Exchange.

    However, that won't help much if you are trying to mix 2 different DB engines except to say that you'll get an error that useful to include in your post to EE.
    LVL 1

    Author Closing Comment

    Dave you friggin rock!

    I am designing a statue in your honour as we speak.

    I learnt from a book and they failed to explain that when using mysqli the connection actually becomes an object (it was a beginners book so can't blame them)

    To anyone else with the same problem. The help manual on this is very sparse so I hope this helps. This is what worked for me:

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Can not connect to Database');
    $query = "INSERT INTO tbl_users (name, middlename, surname, id, email, cell_no, password) VALUES ('".$this->name."',
                            '".$this->middlename."', '".$this->surname."', '".$this->id."','".$this->email."', '".$this->cellNumber."', '')";
                            $result = mysqli_query($dbc, $query) or die("Could not exectute query");
                            $idResult = $dbc->insert_id;
    LVL 1

    Author Comment


    tx I will keep that in mind - I usually type the command in mySQL but using echo to copy paste makes a lot more sense.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    This article describes some very basic things about SQL Server filegroups.
    Creating and Managing Databases with phpMyAdmin in cPanel.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now