Jump to content

Count number of rows in table and get latest row


dc_jt

Recommended Posts

Hi

 

Ive got a table called course_login which takes the staff_id, course_id, ip and login_date of every time a user logs into my system.

 

However I want to count how many times a certain user has logged in and when the last login was. Therefore would I do something like this to get the amount of times?

 

"SELECT COUNT(course_login_id) AS num FROM course_login WHERE staff_id = $iStaffId";

 

Not too sure what the best way is to get the latest one? Is this right?

 

"SELECT * from course_login WHERE staff_id = $iStaffId ORDER by login_date DESC, limit 0,1"

 

Thanks

Link to comment
Share on other sites

Hi

 

Ive got a table called course_login which takes the staff_id, course_id, ip and login_date of every time a user logs into my system.

 

However I want to count how many times a certain user has logged in and when the last login was. Therefore would I do something like this to get the amount of times?

 

"SELECT COUNT(course_login_id) AS num FROM course_login WHERE staff_id = $iStaffId";

 

Not too sure what the best way is to get the latest one? Is this right?

 

"SELECT * from course_login WHERE staff_id = $iStaffId ORDER by login_date DESC, limit 0,1"

 

Thanks

 

Almost u get it.

But if u use the

mysql_num _rows($result)

it will also puul out the number of rows for user.

 

Second Use

desc with limit 1

Hope this will help.

Link to comment
Share on other sites

Thanks for the help, however how would I return the number ?

 

And for some reason it is printing out DESC LIMIT 0,11 when I do:

 

	public function GetCourseLogins($iStaffId, $iCourseId)
{
	$sSql = "SELECT COUNT($this->sPrimaryKey) AS num FROM $this->sTableName
	WHERE course_login.staff_id = '$iStaffId' 
	& course_login.course_id = '$iCourseId'
	ORDER by login_date DESC
	LIMIT 0,1";
	print($sSql);
	return (int) mysql_fetch_object(mysql_query($sSql, $this->oDb->GetConnection()));

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.