views:

21

answers:

2

Hi, 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

A: 

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

$upt = mysql_query("UPDATE counter SET count=count+1");

//extract count from database table $counter = mysql_query("SELECT count FROM counter"); //Display counter. while ($get_count = mysql_fetch_row($counter)) { foreach ($get_count as $values => $field) { echo $field; ?>

This will may help you...

Deepali
I'm leaving it blank because another id will be generated in the database, because i checked the option autoincrement. I want to know how do I get the new generated autoincremented id from the table of the database and print it, because my $.id, is still blank.Thanks for the attention
Marcelo
+1  A: 

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";
Paul Dixon