Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

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.
0
QuintusSmit
Asked:
QuintusSmit
  • 2
1 Solution
 
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
 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now