Jump to content

Insert a blank row


Germaris

Recommended Posts

Hi there!

I wish to insert a blank row in a MySQL table and get as result the id of this row.

Purpose of this operation is to make later an update of this row with collected data.

 

How should I write the query?

 

Many thanks in advance for your help!

 

Best regards,

 

Gerry

Link to comment
Share on other sites

The function you're looking for is mysql_insert_id().

Orio.

 

Thank you very much!

While you were posting I found by myself a less elegant solution but... also working!  :-)

I'm proud!  :-)

 

$table = "mytable";
$query = "INSERT INTO $table VALUES()";
$res = mysql_query($query) or die ("&error=".mysql_error()."<br/>".query);
$querytwo="SELECT * FROM $table";
$querytwo_res=mysql_query($querytwo) or die("&error=".mysql_error()."<br/>".querytwo);
$count=mysql_num_rows($querytwo_res);
print "&count=".$count;

 

But I prefer yours, of course!

 

Best regards,

Gerry

Link to comment
Share on other sites

Finding the number of rows in a database is not an accurate method of finding any information about a row id. Deleting one or more rows make the results incorrect.

 

The method that Orio posted is the correct method of getting the id immediately after an INSERT query.

Link to comment
Share on other sites

Finding the number of rows in a database is not an accurate method of finding any information about a row id. Deleting one or more rows make the results incorrect.

 

The method that Orio posted is the correct method of getting the id immediately after an INSERT query.

 

You are right.

Even if my solution is working I recognize Orio's as more secure !  :-)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.