/    Sign up×
Community /Pin to ProfileBookmark

Getting the last updated row in the mysql database

Ok. I am making a basic adding comments thing for my web. So, using PHP, how do i get the latest updated row from the database, so i can increment up the id by 1, while also adding an ip address and the comment? I also get the latest ip address, so they don’t post 2 times in a row.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208May 03.2008 — Is this what you are looking for?

http://www.php.net/mysql_insert_id

or you could just query the database for the last item.
Copy linkTweet thisAlerts:
@SheldonMay 03.2008 — Why don't you just use an Auto-Increment ID column in the database?

else you could do something like.

[code=php]
<?php

// database connection before hand.

// Query
$sql = "SELECT MAX(id) as id,ipAddress FROM table";
$qry = mysql_query($sql);
$row = mysql_fetch_assoc($qry);
$last = (int)$row['id']++;
$ip = $_SERVER['REMOTE_ADDR'];

// Return Values
echo($last."<br />n"); // New Highest ID
echo($ip."<br />n"); // Visitors IP
if($ip == $row['ipAddress']){
echo("The Visitors IP is the record in the Database<br />n"); // Last IP from Database
}else{
echo("This IP DOES NOT match the Last recorded IP Address<br />n");
}


?>
[/code]
Copy linkTweet thisAlerts:
@WejauthorMay 03.2008 — Why don't you just use an Auto-Increment ID column in the database?

else you could do something like.

[code=php]
<?php

// database connection before hand.

// Query
$sql = "SELECT MAX(id) as id,ipAddress FROM table";
$qry = mysql_query($sql);
$row = mysql_fetch_assoc($qry);
$last = (int)$row['id']++;
$ip = $_SERVER['REMOTE_ADDR'];

// Return Values
echo($last."<br />n"); // New Highest ID
echo($ip."<br />n"); // Visitors IP
if($ip == $row['ipAddress']){
echo("The Visitors IP is the record in the Database<br />n"); // Last IP from Database
}else{
echo("This IP DOES NOT match the Last recorded IP Address<br />n");
}


?>
[/code]
[/QUOTE]



How do i use the auto increment thing?

Say, if i send mysql the comment and ip address, will it automatically increment the id?
Copy linkTweet thisAlerts:
@skywalker2208May 03.2008 — How do i use the auto increment thing?

Say, if i send mysql the comment and ip address, will it automatically increment the id?[/QUOTE]

Yes it will. With each new item being inserted into the database it gets a new unique id.
Copy linkTweet thisAlerts:
@WejauthorMay 03.2008 — Ok, thanks...

I just tried to change the id to auto increment, and it came up with this error:

"#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key "

I know it is the only auto column, but what does it mean by "Defined as a key"?
Copy linkTweet thisAlerts:
@WejauthorMay 03.2008 — Got it....had to make it Primary in the Mysqladmin...Only pressed it cause there is a small picture of a key?

YAY It all works now! Awsome! I just learned the basics of making a forum i think?
×

Success!

Help @Wej spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...