slancasterfreak Posted October 18, 2006 Share Posted October 18, 2006 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 More sharing options...
Barand Posted October 18, 2006 Share Posted October 18, 2006 Ummm, you already know it to be able to specifyWHERE userid='$userid'??? ??? Link to comment Share on other sites More sharing options...
slancasterfreak Posted October 18, 2006 Author Share Posted October 18, 2006 I am updating a row in one table using the userid#, but I need to get the rowid# from that same table/record so that I can update a different table which stores it's data based on rowid# It's a very complex mysql structure and I am simplifying the problem to be able to communicate my need. Link to comment Share on other sites More sharing options...
sanfly Posted October 18, 2006 Share Posted October 18, 2006 Do you have a primary key on the study table? Link to comment Share on other sites More sharing options...
Barand Posted October 18, 2006 Share Posted October 18, 2006 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 More sharing options...
slancasterfreak Posted October 18, 2006 Author Share Posted October 18, 2006 Yes sanfly, I have primary auto-incrementing keys on the tables in question, and this field id is the thing I am after.Thanks for your help sanfly, and your good attitude sanfly Link to comment Share on other sites More sharing options...
redarrow Posted October 18, 2006 Share Posted October 18, 2006 Set a session for the users id then use it.You have already been told the ansaw read ok. Link to comment Share on other sites More sharing options...
sanfly Posted October 18, 2006 Share Posted October 18, 2006 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 isIs that what you were after? Link to comment Share on other sites More sharing options...
slancasterfreak Posted October 18, 2006 Author Share Posted October 18, 2006 AHHH I could KISS you you sweet man!!! (if you're a man)Thanks for your HELP!!!! Link to comment Share on other sites More sharing options...
sanfly Posted October 18, 2006 Share Posted October 18, 2006 Not a man, and your thanks is thanks enough :) Link to comment Share on other sites More sharing options...
redarrow Posted October 18, 2006 Share Posted October 18, 2006 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 More sharing options...
slancasterfreak Posted October 18, 2006 Author Share Posted October 18, 2006 well then just another example of how great gals are! thanks! Link to comment Share on other sites More sharing options...
slancasterfreak Posted October 18, 2006 Author Share Posted October 18, 2006 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 More sharing options...
Barand Posted October 18, 2006 Share Posted October 18, 2006 Before you gals get too excited I suggest you test Sanfly's codeAn update query doesn't return a resultset so this bit will fail[code]$rowg = mysql_fetch_array($resultg));$study_id = $rowg['study_id'];[/code] Link to comment Share on other sites More sharing options...
Recommended Posts