简体   繁体   中英

Echo autoincrement id doubt

can I print the id, even if it's autoincrement ? Because the way I'm doing I'm using an empty variable for id.

$id= "";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die ("Não conectou com a base $database");

mysql_query("INSERT INTO table1(id,...)
VALUES ('".$id."',....)")
or die(mysql_error());

mysql_close();

echo "Your id is :";

echo "".$id;

I'm trying to print the id, but it's coming blank. I checked the table and there's an id number there. How can I print it then at?

Thanks for the attention

You can retrieve the last auto-generated id on a connection with mysql_insert_id()

Your sample should look like this:

mysql_query("INSERT INTO table1(...) VALUES (....)")
   or die(mysql_error());

$id=mysql_insert_id();

echo "Your id is : $id";
echo "Blah Blah Blah". mysql_insert_id() ;

First insert a $id into database as 0. Here I am using $id = count and $field will print value

$dbconn = mysql_connect($host,$user,$pass) or die (mysql_error()); // try to connect to database mysql_select_db($db,$dbconn); mysql_query("INSERT INTO counter ( count ) VALUES (\\'0\\')"); mysql_close($dbconn);

Then update it

$field) { echo $field; ?>

This will may help you...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM