Jump to content

Archived

This topic is now archived and is closed to further replies.

slancasterfreak

when updating mysql, get the row# easily?

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

Share this post


Link to post
Share on other sites
Ummm, you already know it to be able to specify

WHERE userid='$userid'

??? ???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Set a session for the users id then use it.

You have already been told the ansaw read ok.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Not a man, and your thanks is thanks enough  :)

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Before you gals get too excited I suggest you test Sanfly's code

An update query doesn't return a resultset so this bit will fail
[code]
$rowg = mysql_fetch_array($resultg));
$study_id = $rowg['study_id'];
[/code]

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

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.