0

I have a table tracking_table which will update the user lan and log at regular intervals and the data table will be like below:

Id  Username    Log     Lan   Timestamp
1   User1      1.555        3.55       12:00PM
2   User2       3.55        4.55       12:10PM
3   User1       6.55    8.66       1:30PM
4   User2       7.88        9.68    2:10PM

The same user data will be updated multiple times.

So, can I use select * from tracking_table where username="user1" to retrieve all the lans and logs of that particular user from all the rows?

1
  • 2
    Why don't you try? Commented May 25, 2018 at 8:02

1 Answer 1

1

Yes, you basically answered your own question. However, there's a slight complication. While you were on the right track, your logical solution for what you use in your WHERE clause could be problematic, since you use username. Now, you did specify "same" user. But! What do you do when different people have the same name? Then you'll have problems. The best would be to have a user id to go by. That will be unique and ensure that you will retreive data from that user only.

//You'll have to implement a userId field in your table(s)
SELECT * FROM tracking_table WHERE userId='1';

Since you added PHP to your tags, I'm wondering if you need to use it in PHP later?

In that case, depending on your syntax (mysql_ / mysqli_ / PDO) you can store them into PHP variables for later use.

mysql_ syntax:

<?php
//Note that mysql_ is deprecated. I simply just included this in case.
//Select statement
$sql="SELECT * FROM tracking_table WHERE userId='1'";
//Result set from your select
$result=mysql_query($sql);
//The rows of data
$row=mysql_fetch_array($result);

//Row data stored as variables
$id=$row['Id'];
$username=$row['Username'];
$log=$row['Log'];
$lan=$row['Lan'];
$timestamp=$row['Timestamp'];
?>

mysqli_ syntax:

<?php
/*
$conn is the database connection variable from you config.php
(assuming that's the name of the include file for your database connection)
*/
//Select statement
$sql="SELECT * FROM tracking_table WHERE userId='1'";
//Result set from your select
$result=mysqli_query($conn, $sql);
//The rows of data
$row=mysql_fetch_array($conn, $result);

//Row data stored as variables
$id=$row['Id'];
$username=$row['Username'];
$log=$row['Log'];
$lan=$row['Lan'];
$timestamp=$row['Timestamp'];
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you providing the solution and pointing out the logical error. And yes, i'm planning to use it with php. Also thank you for the syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.