Jump to content

when updating mysql, get the row# easily?


slancasterfreak

Recommended Posts

Hi,
I've used this before:

[code]
$query = "INSERT INTO myusers SET lname='$lname', fname='$fname'";
mysql_query($query)  or die('Query failed: ' . mysql_error());

//GET ID FOR LAST INSERT
$id = mysql_insert_id();
[/code]

to get a row id so I can use it after an insert, but is there anything similar for easily getting a row id after an update like this?

[code]
$queryg = "UPDATE study SET answers='$answerarray' WHERE userid='$userid'";
$resultg = mysql_query($queryg) or die('Query failed: ' . mysql_error());
[/code]

I need the row # from this Updated record to alter a different table in my database, and wondered if there is an easy way to get it, or do I have to just get it by doing yet another query?

Thanks in advance!
Sally
Link to comment
Share on other sites

No, it doesn't make any sense at all. The physical row location in a database is irrelevant. When you insert a record and get the result from mysql_insert_id() that is the value of the auto_increment column and may, coincidentally be the number of the physical row in most cases but record id #103 could just as easily be on physical row 3. You should design an application that  will work even if record id's are scattered randomly throughout the table - as some can be.

Use record id and nor row numbers.
Link to comment
Share on other sites

After this code

[code=php:0]$queryg = "UPDATE study SET answers='$answerarray' WHERE userid='$userid'";
$resultg = mysql_query($queryg) or die('Query failed: ' . mysql_error());[/code]

use this to retrieve the id of the row

[code=php:0]$rowg = mysql_fetch_array($resultg));
$study_id = $rowg['study_id'];[/code]


Replace study_id with whatever the name of your primary key field is

Is that what you were after?
Link to comment
Share on other sites

sanfly that ok for the page that got the update code within it, but if he goes to any other page here need to use a session or cookie for that users id.

so unless this is a one page php code design this idear he's got is a flaw.

it would be inpossable to design a proper dynamic php website using the users method in my option without the use off sessions or cookies.

according to the title of the post a simple solution is to use sessions.
Link to comment
Share on other sites

redarrow, I'm sorry but your help was not helpful, I didn't need your commentary on my internal PHP structure, and actually never even described it to you, so it is haughty at best for you to decide that you have some sort of answer for me beyond the confines of my question. You are assuming that you know something about my site that you don't know. I have been helped thank you. goodbye now.
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.