I have a table named 'transactions', in which i store a user's all transactions. i want an mysql query such that it would extract recent 3 transactions from table for a specific user id. i know i can use limit.
SELECT * FROM 'transactions' WHERE 'userid'=20 LIMIT 0,3;
but how can i access attributes of different transactions which are returned as object after query? also using limit 0,3 would start search from start of table but i want to start search from bottom of table. I am using it with php.
Well, since you are not using a programming language, and just mysql
, all your attributes would be displayed in the query result in the console window.
Edit:
To access your query and results, see the following article on PHP.net's website: http://www.php.net/manual/en/mysqli-result.fetch-assoc.php
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)
", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
/EndEdit
To sort newest first, assuming you have a id
column, use ORDER BY
:
SELECT * FROM 'transactions' WHERE 'userid'=20 ORDER BY id DESC LIMIT 0,3;