mysqli insert_id

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paynod
Forum Newbie
Posts: 1
Joined: Thu Mar 31, 2011 2:53 pm

mysqli insert_id

Post by paynod »

I'm trying to retrieve the mysqli insert_id from an INSERT database query but each time the result is 0. PLease can someone point me in the right direction as to what i'm doing wrong. Here is my query

Code: Select all

       $mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
	$this->execSQL("INSERT INTO itm_pic_detail(itm_pic_name, itm_pic_type, itm_pic_size) VALUES (?,?,?)", array('sss', $filename, $type, $size),true);
	$mysqli->insert_id;
	$message = "$filename uploaded successfully";
	}
	if ($this->_renamed) {
	$message .= " and renamed $name";
	}
	$this->_messages[] = $message;
	} else {
    $this->_messages[] = 'Could not upload ' . $filename;
	}
	  }
	}
[ /syntax]
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysqli insert_id

Post by social_experiment »

The Manual wrote:The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.
Do you have an auto-imcrementing column in your table?
P.S No " " are needed when using the PHP Code button.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply