I have a table named character_level:
level experience
1 0
2 998
3 2994
4 5988
and so on.
I have another table user_Character¨
ID character_current_xp
1 150
2 35
to check if level must be changed I do this:
$xp = mysqli_query($con,"SELECT character_current_xp FROM user_character WHERE ID = '$currentUser'");
while($row = mysqli_fetch_array($xp))
{
$current_xp = $row['character_current_xp'];
}
$lvl2 = mysqli_query($con,"SELECT level FROM character_level WHERE experience <= $current_xp order by experience desc limit 1");
while($row = mysqli_fetch_array($lvl2))
{
$level_after = $row['level'];
}
if(isset($_POST['accept_xp'])){ //with this i check ressults
$setxp = "UPDATE user_character SET character_current_xp = character_current_xp+100, character_current_lvl = $level_after WHERE ID = $currentUser";
mysqli_query($con, $setxp);
mysqli_close($con);
header('Location: account.php');
}
Problem is wheither i use <= $current_xp order by experience desc limit 1 weither >= $current_xp order by experience desc im still off by one level.
Anyone can shoot me anything?
Thanks in advance.
Chris
Well, here's something:
SELECT l.level
FROM user_character c
JOIN character_level l
ON l.experience <= c.character_current_xp
WHERE ID = '$currentUser'
ORDER
BY experience DESC
LIMIT 1
Related
I am trying to figure out how to see how many times in the past 7 entries/rows that sleep = 1.
Currently, $num shows the number of times sleep = 1 in all rows. I have seen that 'order by xxx desc limit 7' has been suggested in other answers but it doesn't seem to work well in this scenario. Would greatly appreciate any help, thanks!
Heres my code:
$result = mysqli_query($conn, "SELECT count(*) FROM test_table WHERE sleep = 1");
$row = mysqli_fetch_row($result);
$num = $row[0];
You can try this one:
SELECT a, COUNT(b) FROM test_table
WHERE sleep = 1
GROUP BY a
ORDER BY COUNT(b) ASC
LIMIT 7
Here, a is the name of your column you are trying to count
And, b is any column name for usage to count (it can id, or any column name)
If the sleep is binary/tinyint you can just sum that in the query with the order by.
SELECT sum(sleep)
FROM table
ORDER BY COUNT(id) DESC
LIMIT 7
If sleep isn't binary you can use a case statement.
SELECT sum(case when sleep = 1 then 1 else 0 end) as totalsleep
FROM table
ORDER BY COUNT(id) DESC
LIMIT 7
Here's my idea, get all the data in test_table and create a loop that will count the sleep, like this
$result = mysqli_query($link, "SELECT sleep FROM test_table;");
$x=1;
$sleep = [];
$SleepCount= 0;
while($row = mysqli_fetch_array($result)) {
if($row[0] == "1"){
$SleepCount++;
}
if($x == 7){
array_push($sleep,$SleepCount);
$SleepCount = 0;
$x=0;
}
$x++;
}
echo "<pre>",print_r($sleep),"</pre>";
I have a MySQL DB that resembles the following:
uid suid
1 5
1 6
2 5
5 1
5 2
I am giving it a single unique "uid" via the POST method, call it 1. What I need to do is return all "suid" where $uid "has" suid AND suid (as uid) "has" $uid (as suid.) So, in the above example, the script should only return 5.
I know my first step is
"Select * FROM table Where uid = $uid"
then maybe I have to loop through the results and query the DB WHERE suid = $uid.
I do not know how to do the second query. Any suggestions?
One option here would be to self join, with the join condition being that uid in one table matches suid in the other table, and vice-versa for the suid in the first table.
SELECT
t1.suid
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.uid = t2.suid AND
t1.suid = t2.uid
WHERE
t1.uid = 1
Before applying the WHERE clause, the above query would return two records:
uid | suid (uid not selected)
1 | 5
5 | 1
The WHERE clause then chooses the first record, which is what we want, using the uid parameter which you pass it.
Demo here:
Rextester
this is what I came up with:
$uid = $_POST["UID"];
$myquery = "SELECT * FROM Table WHERE uid = '$uid'";
$result = $conn->query($myquery);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myquery2 = "SELECT * FROM Table WHERE uid = '" . $row['suid'] . "' AND suid = '$uid'";
$result2 = $conn->query($myquery2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
echo $row["uid"].
"!##$";
}
}
}
}
$conn->close();
?>
I feel it is not pretty but it did the trick.
I thought this would be simple but I'm having a tough time figuring out why this won't populate the the data array.
This simple query works fine:
$queryPrice = "SELECT price FROM price_chart ORDER BY id ASC LIMIT 50";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
But instead I want it to choose the last 10 results in Ascending order. I found on other SO questions to use a subquery but every example I try gives no output and no error ??
Tried the below, DOESN'T WORK:
$queryPrice = "SELECT * FROM (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
I also tried specifying the table name again and using the IN, also doesn't work:
$queryPrice = "SELECT price FROM price_chart IN (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
In both examples my array is blank instead of returning the last 10 results and there are no errors, so I must be doing the subquery wrong and it is returning 0 rows.
The subquery doesn't select the id column, so you can't order by it in the outer query. Also, MySQL requires that you assign an alias when you use a subquery in a FROM or JOIN clause.
$queryPrice = "SELECT *
FROM (SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) x ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice) or die (mysqli_error($conn));
$data = array();
while ($row = mysqli_fetch_assoc($resultPrice)) {
$data[] = $row['price'];
}
You would have been notified of these errors if you called mysqli_error() when the query fails.
Your second query is the closest. However you need a table alias. (You would have seen this if you were kicking out errors in your sql. Note you will need to add any field that you wish to order by in your subquery. In this case it is id.
Try this:
SELECT * FROM (SELECT price, id
FROM price_chart ORDER BY id DESC LIMIT 10) as prices
ORDER BY id ASC
You must have errors, because your SQL queries are in fact incorrect.
First, how to tell you have errors:
$resultPrice = mysqli_query (whatever);
if ( !$resultprice ) echo mysqli_error($conn);
Second: subqueries in MySQL need aliases. So you need this:
SELECT * FROM (
SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) AS a
ORDER BY id ASC";
See the ) AS a? That's the table alias.
This is my code.
$sqlcount = "SELECT count(*) AS C, Horse_ID FROM images WHERE Horse_ID = 24 GROUP BY Horse_ID HAVING COUNT(*) > 1 LIMIT 0, 30";
//echo $sqlcount;
$resultcount = $conn->query($sqlcount);
$rowcount = $result->fetch_assoc();
echo $rowcount['C'];
Why won't it echo the number 4, which is what shows when I test it in phpmyadmin? There are 4 duplicate values in that table hence the 4.
$rowcount = $result->fetch_assoc();
to
$rowcount = $resultcount->fetch_assoc();
If you want the number of duplicates in the database, why not write the query to get that value?
SELECT COUNT(*)
FROM (SELECT count(*) AS C, Horse_ID
FROM images
WHERE Horse_ID = 24
GROUP BY Horse_ID
HAVING COUNT(*) > 1
) i;
Then, you will only be returning one value from the database to the application (which is faster) and there is no need to artificially limit the count to 30.
Here's my current code:
<?php
$tsql = " ";
$tmpsort = " ORDER BY b.flag,b.jjloveb DESC,b.yhtime ";
$rt=$db->query("SELECT a.nickname, a.sex, a.grade, a.photo_s, a.photo_f, a.photo_pass, b.id,b.userid, b.datingkind, b.title, b.price, b.yhtime, b.maidian, b.content, b.bmnum, b.click, b.flag, b.jjloveb, a.birthday, b.province, b.city, b.area, b.age1, b.datingkind, a.identityproof
FROM ".__TBL_MAIN__." a,".__TBL_DATING__." b
WHERE $tsql b.flag>0 AND b.userid=a.id AND a.flag=1
$tmpsort
LIMIT 6");
$total = $db->num_rows($rt);
for($i=0;$i<$total;$i++) {
$rows = $db->fetch_array($rt);
if(!$rows) break;
$Uid = $rows[7];
$Unickname = badstr($rows[0]);
$Usex = $rows[1];
What I want is to fetch the records from the same user id only once.
Thanks very much!
$rt=$db->query("SELECT a.nickname,a.sex,a.grade,a.photo_s,a.photo_f,a.photo_pass,b.id,b.userid,b.datingkind,b.title,b.price,b.yhtime,b.maidian,b.content,b.bmnum,b.click,b.flag,b.jjloveb,a.birthday,b.province,b.city,b.area,b.age1,b.datingkind,a.identityproof FROM ".__TBL_MAIN__." a,".__TBL_DATING__." b WHERE $tsql b.flag>0 AND b.userid=a.id AND a.flag=1 group by b.userid $tmpsort LIMIT 6");
I achieved what I wanted by just inserting a group by. Yeah!
But what about if 2 occurrences per userid is wanted, what approach should be taken?