mysql_insert_id() gives a 0 return

Hi

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.
LVL 1
QuintusSmitAsked:
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.

Dave BaldwinFixer of ProblemsCommented:
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.

http://us.php.net/manual/en/mysqli-stmt.insert-id.php  Please read the notes and comments for another function to do this.
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
Hugh McCurdyCommented:
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.
0
QuintusSmitAuthor Commented:
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;
0
QuintusSmitAuthor Commented:
@hmccurdy

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.
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
Databases

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.