Getting highest score from mySQL database - mysql

I have a database set up:
and the resulting table:
I am trying to retrieve data from the database so that the output displays the players according to who has the highest score in descending order. However, I get the following output:
Pollywinkle Anderson has a high score of 600 recorded in the database yet only her score of 200 is shown. How can I retrieve each players highest score? The score is stored as VARCHAR and I read in solutions to similar questions that it needs to be converted to an INT. I have tried to do that but it's still not working. I am very new to programming in general, any help would be appreciated. My code is below. Thank you.
<?php
$sql = "SELECT * , COUNT(*) AS `phone` FROM `players` GROUP BY
CONCAT(`fname`, `surname`) ORDER BY MAX(CONVERT(`score`,INT)) DESC";
$result = mysqli_query($conn,$sql)or die(mysqli_error());
echo "<table>";
echo "<tr><th>Firstname</th><th>Surname</th><th>Email</th><th>Score</th>
<th>Number of Submissions</th></tr>";
while($row = mysqli_fetch_array($result)) {
$fname = $row['fname'];
$surname = $row['surname'];
$email = $row['email'];
$score = $row['score'];
$phone = $row['phone'];
echo "<tr><td style='width: 200px;'>".$fname."</td><td style='width:
200px;'>".$surname."</td><td style='width: 200px;'>".$email."</td><td
style='width: 200px;'>".$score."</td><td style='width: 200px;'>".$phone."
";
}
echo "</table>";
mysqli_close($conn);
?>

You can try below:
SELECT fname,surname,MAX(CONVERT(`score`,INT)) as score, COUNT(*) AS `submission`
FROM `players`
GROUP BY fname,surname
ORDER BY score desc

If you want to list your players ordered by their score you don't need a count or to group by anything. All you need to do is a simple select query with an order by, like:
select * from players order by score

Related

MYSQL Query fail to select possible result

I want to get all emp_ids of employees which is manager_id equal to login user_id. But why this query show only first result of the column. I could not able to find the problem.
$userID=$_SESSION['userID'];
var_dump($userID);
$result1 = mysql_query("SELECT emp_id FROM employee where manager_id='$userID' ORDER BY emp_id");
$array = mysql_fetch_array($result1);
$id1=$array['emp_id'];
Please help !
You should loop over the result
and eg: you can store all the empID in an array
$cnt = 0;
while ($row = mysql_fetch_array($result1)) {
echo "ID: " . $row[0] ;
$myArrayOfEmpID[$cnt] = $row[0];
$cnt++;
}
instead you only set
$array = mysql_fetch_array($result1);
$id1=$array['emp_id'];

Few warnings [expects parameter]

I've been trying to display the number of rows using this code but it keep says
1 Rows which is wrong
<?php
$link = mysql_connect("localhost", "gamin1_forum", "password123");
mysql_select_db("gamin1_forumdb", $link);
$result = mysql_query("SELECT COUNT(*) FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows";
?>
Rows are approximately 1443 but it kept saying 1
The Count(*) returns you one row which contains the number of rows as a value.
By using mysql_num_rows($result) you are actually counting the amount of rows of the Count(*) result which really is one.
Change it to:
$result = mysql_query("SELECT * FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
Or just use the Count(*) value (which is probably better since it count in the DB and not retrieving the whole table for it) using mysql_fetch_array.
This is true, mysql_num_rows() is telling that there was one "row" returned. You need to check the value of the returned row to get your count. Try this
$link = mysql_connect("localhost", "gamin1_forum", "password123");
$result = mysql_query("SELECT COUNT(*) as cnt FROM smf_personal_messages", $link);
$row = mysql_fetch_array($result);
echo ($row['cnt']);
On a side note, you should not use mysql_* functions for security reasons. Try PDO/mysqli_* functions

mysql_insert_id() not giving value

here is the code that i am using, the code is simply getting the same value by two different methods, but the mysql_insert_id() echo is not giving the output.
The value given by -> echo mysql_insert_id(); is 0
Can anyone tell me the possible reason and how to resolve this problem?
include'config.php';
$query = "SELECT id
FROM users
ORDER BY id DESC
LIMIT 1";
if($query_run = mysql_query($query))
{
$query_result = mysql_fetch_row($query_run);
$id = $query_result[0];
echo $id;
echo $lastId = mysql_insert_id().'<br />';
//'Query Successful!';
} else {
echo mysql_error();
}
If you are looking for the next auto increment id that will be inserted. I guess you should try this.
$query_autoinc="SHOW TABLE STATUS LIKE 'mytable'";
$exec_autoinc=mysql_query($query_autoinc);
$row_autoinc=mysql_fetch_assoc($exec_autoinc);
$inserted_id = $row_autoinc['Auto_increment'];
Or if you want the last inserted id, you might try this.
$query= "SELECT max(id)
FROM users
ORDER BY id DESC
LIMIT 1";

Mysql and php - compare them and bring back one out of the two values

Category Make
BUS hiace
Car honda
bus hiace
Expected Result:
bus (hiace) - in the drop down list
car (honda) - in a different drop down list
<SCRIPT TYPE="TEXT/JAVASCRIPT">
function setup(ans) {
lit = ''
if (ans == 'bus') {
<?php
$result = mysql_query("select Category from auto
group by Make"); ?>
lit = lit+ '<select name="q4" ONCHANGE="alert(document.quest.q4.value)" style="width:130px; background-color:#FFCC33; font-weight:bold; font-size:12px;" >'
lit = lit+ '<option>MAKE</option>'
<?php while($row = mysql_fetch_array($result))
{
$make = $row['Make'];
//print $make;
?>
var make = "<?php echo $make; ?>"
lit = lit+ '<option>'+make+'</option>'
<?php }?>
lit = lit+ '</select>'
}
</script>
For a random ID of the ones matching, try
SELECT * FROM table_name GROUP BY Name;
or if you want the maximum ID with the same name
SELECT MAX(ID),Name FROM table_name GROUP BY name;
This naturally works with MIN() too if you want the first ID.
SELECT id,name FROM tbl_names GROUP BY name ORDER BY id ASC ?
select min(ID), Name
from tablename
group by Name
SELECT *
FROM table
GROUP BY Name
SELECT *
FROM table
where ID IN (select MIN(ID)
FROM table
GROUP BY Name)

Check whether value exist in database

What's the best way to check whether the value is in the database?
Am I doing it correct?
$result = mysql_query("SELECT COUNT(*) FROM table WHERE name = 'John'");
$count = count($result);
you could use straight forward ,
mysql_num_rows() ;
eg :
$con = mysql_connect($host,$uname,$passwd)
mysql_select_db($dbase,$con);
$result = mysql_query($query,$con);// query : SELECT * FROM table WHERE name='jhon';
if( ! mysql_num_rows($result)) {
echo " Sorry no such value ";
}
Yes you are doing it right, if you are only concerned with checking if there are any records where name='john'
SELECT COUNT(*) FROM table WHERE name = 'John'
will return the no. of records where name field is 'John'. if there are no records then it will return 0, and if there are any records it will return the number of records.
But the above query will miss the entries where name is 'John Abraham' or 'V john', to include even these
you can modify your query like this.
SELECT COUNT(*) FROM table WHERE name like '%John%'
I'd say yes.
$result = mysql_query("SELECT COUNT(*) AS 'nb' FROM table WHERE name = 'John'");
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$count = $line['nb'];
Will give you the number of matching rows.
$result = mysql_query("SELECT COUNT(*) as user FROM table WHERE name = 'John'");
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$count = $line['user'];
if($count!=0)
{
echo "user exists";
}
else
{
echo "There is no such user";
}