I want to have a "toplist" type thing for my forum. I have a kill log for a pc game. So in my sql table 'victims' i have couple of rows for data. I want to take to use $killforumid to sum $score for the entire table. there are multiple entries with the same $killforumid, and i want to sort with that and add up the score for that user.
It would look like this:
User 1 = 1000 points, 60 kills
User 2 = 5000 points, 100 kills
PHP code:
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT score, killforumid FROM hcvictims";
$result = $conn->query($sql);
if you're using RightClick query (as i'll advise):
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT sum(score), killforumid FROM hcvictims GROUP BY killforumid ORDER BY sum(score) DESC";
$result = $conn->query($sql);
while($row = $result->fetch_row())
{
echo "killforumid: " . $row[1] . ", score : " . $row[0] . "\n";
}
/* free result set */
$result->free();
see: mysqli-result object,
fetch-row method
Related
i am trying to display some values from database to my html calling the last inserted id
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from registers where id= LAST_INSERT_ID()";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " $row[firstname] ";
}
} else {
echo "0 results";
}
But when I am using this, the result is not being displayed, is there any problem with my code, how do I retrieve the last inserted id from sql?
try this query
$sql = "SELECT * from registers ORDER BY ID DESC limit 1 ";
it will give you only one result and this result will be the last inserted ID.
But ID needs to be auto increment.
To retrieve the last id in the table you need:
SELECT id FROM registers ORDER BY ID DESC LIMIT 1
I am relatively new to PHP and SQL and I am creating a simple blog tool for my own page and I'm stuck on how I can make a comment section.
I have 2 tables
general, which contains the blog post and additional information
comments, which contains comments
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error){
die ("connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM general WHERE hidden = 'false' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while ($row = $result->fetch_assoc()){
echo "<div class='blogpost'>
<div class='blogbody'></div>";
//I need to do a query into the comments table at this point to find
//all comments matching the criteria to be matched with the post here.
echo "<div class='comments'></div></div>";
}
}
else{
echo "no posts";
}
I manage to get the blogposts working but as soon as I try a second query by adding
$conn2 = new mysqli($servername, $username, $password, $dbname);
if ($conn2->connect_error){
die ("connection failed: " . $conn2->connect_error);
}
$sql2 = "SELECT * FROM comments WHERE $row["parent"] ORDER BY id DESC";
$result2 = $conn2->query($sql2);
if ($result2->num_rows > 0){
while ($row2 = $result2->fetch_assoc()){
}
}
else{
echo "no comments";
}
it all just breaks. I am not able to throw in a loop that gets all the comments for the specific post before it starts loading additional posts.
How would I go on about solving this? I might need a little spoonfeeding with comments in any code samples,
You can use the primary key of the post and that can be used in comments table as a foreign key so that you can easily make your query with that will show all the comments against that specific post. Then the query will be like this:
$postid=$row['post_id'];
$sql2 = "SELECT * FROM comments WHERE post_id='$postid' ORDER BY post_id DESC";
Changing the second query to fixed the problem.
WHERE $row["parent"] ORDER. Change that to WHERE parent={$row['parent']} ORDER
Thanks to Jeff for providing this answer
I've got a table with two columns "Number" and "Date". I want retrieve the value of the last row however this isn't working:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Number FROM functions_order_numbers ORDER BY Number DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row["Number"];
}
mysqli_close($conn);
Table:
Table Screenshot
I'm trying to create an incremental unique order number
If you want an incremental number in the table, then use auto_increment:
create table t (
number int auto_increment primary key, -- usually it is a primary key
. . .
);
Don't attempt to do this at the application layer. It is dangerous, because multiple updates at the same time might not do the right thing.
I am trying to make simple page which will return values from MySQL table, but the problem is that if I want to use condotions in query then it doesn't work.
My PHP page:
<?php
$servername = "10.10.10.10";
$username = "username";
$password = "password";
$dbname = "GENERIC_TABLES";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT WO_NUM+1 from GENERIC_TABLES.WO_NUMBERS ORDER BY WO_NUM DESC limit 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> WO Number ". $row["WO_NUM"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
So, WO_NUM column has numbers like 1, 2, 3 etc.
I want to get the last one + 1
So if I do like:
$sql = "SELECT WO_NUM from GENERIC_TABLES.WO_NUMBERS ORDER BY WO_NUM DESC limit 1";
Then it works fine, but if I want to make it WO_NUM + 1 then it returns nothing.
Why it happens like that and is there any way to get what I want using MySQL?
I don't want to get WO_NUM and then using PHP make it + 1, since I also need INSERT to the table values and I would like to understand why it doesn't work.
As you realized, WO_NUM+1 changes the column name in the resulting array, so use an alias WO_NUM+1 as NEW_NUM. However I would not bother with the sorting and limit. Consider MAX():
SELECT max(WO_NUM)+1 as NEW_NUM from GENERIC_TABLES.WO_NUMBERS
As it has been pointed by AbraCadaver, I missed that if I am using WO_NUM + 1 then column name changing so that is why i didn't get any output, so using alias solved problem.
I am new at this and learning the code.
I want to create the php code which select the particular row.
say 5th row or 6th row any row.
I create the code like this
<?php
$servername = "localhost";
$username = "test1";
$password = "pass";
$dbname = "test1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT FIELD1, FIELD2 FROM mytable ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["FIELD1"]. " - Name: " . $row["FIELD2"]. " <br>";
}
} else
{
echo "0 results";
}
$conn->close();
?>
THis code works fine but it give the all data of the table.I want to just select particular row number data.how to do this??
You can do it with the LIMIT statement, say LIMIT 3,1 to select the 4th row. First number is the starting row, second number is the count of rows to select.
$sql = "SELECT FIELD1, FIELD2 FROM mytable LIMIT $row_index, 1";
will give you row $row_index + 1
You can do it using WHERE condition in query as SELECT FIELD1, FIELD2 FROM mytable WHERE id = 1.