This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 years ago.
Guys I'm new here and new to MySQL too ..
So I am trying to create a database which manages a team record.The database contains a table named team with a set of columns as follow,
TeamID
TeamRank
TeamName
TeamWins
TeamLoss
TeamPoints
So the agenda is to rank the teams on the basis of points, the greater the points higher the rank.
<?php
$con = mysqli_connect("", "", "", "");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SET #rownum := 0;
INSERT INTO team( TeamRank, TeamName)
SELECT #rownum := #rownum + 1 AS TeamRank, TeamName
FROM (SELECT SUM(TeamRank)AS TeamRank , TeamName
FROM team
GROUP BY TeamName
ORDER BY TeamRank DESC) as result
ON DUPLICATE KEY UPDATE TeamName = VALUES(TeamName);"
);
echo "<table border='1'>
<tr>
<th>Rank</th>
<th>TeamID</th>
<th>TeamName</th>
<th>Total Points</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['TeamRank'] . "</td>";
echo "<td>" . $row['TeamID'] . "</td>";
echo "<td>" . $row['TeamName'] . "</td>";
echo "<td>" . $row['TeamPoints'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
Im recieveing this Error
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
Where am I Wrong? If there a problem with the code please guide me.
P.S[EDIT]: All I want to do is take TeamName,TeamWins,TeamLoss and TeamPoints as input, and as the TeamPoints increase/decrease of a team its supposed to move the rank upwards/downwards and display a Ranking table.
You are using mysqli_query for multi query. so you have to use mysqli_multi_query.
change your code to:
<?php
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SET #rownum := 0;
INSERT INTO team( TeamRank, TeamName)
SELECT #rownum := #rownum + 1 AS TeamRank, TeamName
FROM (SELECT SUM(TeamRank)AS TeamRank , TeamName
FROM team
GROUP BY TeamName
ORDER BY TeamRank DESC) as result
ON DUPLICATE KEY UPDATE TeamName = VALUES(TeamName);
";
echo "<table border='1'>
<tr>
<th>Rank</th>
<th>TeamID</th>
<th>TeamName</th>
<th>Total Points</th>
</tr>";
if (mysqli_multi_query($con,$query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($con)) {
while ($row = mysqli_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row['TeamRank'] . "</td>";
echo "<td>" . $row['TeamID'] . "</td>";
echo "<td>" . $row['TeamName'] . "</td>";
echo "<td>" . $row['TeamPoints'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
}
} while (mysqli_next_result($con));
}
echo "</table>";
mysqli_close($con);
?>
Related
I have 3 tables i want to display the 3 table data in single table based on primary key, foreign key the result came perfectly! But i need to calculate rank based on the total marks from my second table.
result screenshot:
Please anyone tell me the query to calculate rank
I used the following mysql query
if(isset($_POST['submit']))
{
$result = mysqli_query($con,"
SELECT s.student_name
, s.contact_number
, m.total
, m.rank
, p.father_name
FROM student_details s
JOIN mark m
ON s.student_id = m.student_id
JOIN parents_details p
ON p.student_id = s.student_id
WHERE s.student_name = '".$_POST['student_name']."'
");
echo "<table border='1' align='center' cellpadding='15' bgcolor='#FFFFFF'>
<tr>
<th>NAME</th>
<th>CONTACT NUMBER</th>
<th>TOTAL MARK</th>
<th>RANK</th>
<th>FATHER NAME</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['student_name'] . "</td>";
echo "<td>" . $row['contact_number'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['father_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
}?>
SELECT * FROM
(
SELECT #rank := #rank+1 finalrank,ZZ.* FROM
(
SELECT student_details.student_name,
student_details.contact_number, mark.total,
mark.rank, parents_details.father_name
FROM student_details
INNER JOIN mark ON student_details.student_id=mark.student_id
INNER JOIN parents_details ON parents_details.student_id=student_details.student_id ,(SELECT #rank:=0)z
ORDER BY mark.total desc
)ZZ
)ZZZ
WHERE ZZZ.student_name = '".$_POST['student_name']."'
Just try above query.
Here I had used SELECT #rank:=0 and #rank := #rank+1.
This question already has answers here:
ROW_NUMBER() in MySQL
(26 answers)
Closed 6 years ago.
The code below is returning a table with data from mysql, but what I'm missing in the output is a generated column with a rank based on a column's data (wilks in this example). I've looked for examples and answers to similar questions, but I can't get it to work.
In short: I need an extra column next to the ID column where a rank beginning from 1 is displayed, based on the data from the Wilks column.
Thank you so much!
<?php
$con=mysqli_connect("localhost", "user", "password", "dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM `Mannen` ORDER BY `Wilks` DESC");
echo "<table border='1'>
<tr>
<th>#</th>
<th>Naam</th>
<th>Lichaamsgewicht</th>
<th>Vereniging</th>
<th>Squat</th>
<th>Bench</th>
<th>Deadlift</th>
<th>Totaal</th>
<th>Wilks</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Naam'] . "</td>";
echo "<td>" . $row['Lichaamsgewicht'] . "</td>";
echo "<td>" . $row['Vereniging'] . "</td>";
echo "<td>" . $row['Squat'] . "</td>";
echo "<td>" . $row['Bench'] . "</td>";
echo "<td>" . $row['Deadlift'] . "</td>";
echo "<td>" . $row['Totaal'] . "</td>";
echo "<td>" . $row['Wilks'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
First, if you just want an enumerated value, you can get that using PHP.
But, it is easy enough in MySQL, using variables:
SELECT m.*, (#rn := #rn + 1) as rank
FROM Mannen m CROSS JOIN
(SELECT #rn := 0) params
ORDER BY Lichaamsgewicht DESC;
Technically, this implements the related function of row_number() rather than rank(). Your question is unclear on what you mean by "rank".
I need my computer game to take all kills and deaths from a user's total and then make it so the kills are divided by the deaths, then that total is put at the end. This ratio is known as their kill-death ratio, or "KDR".
<?php
// Create connection
$con=mysqli_connect("ipaddress","user","password","minecraft");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT *, `kills`/`deaths` as `KDR` FROM war_kills ``ORDER BY kills DESC");
echo "<table border='1'>
<tr>
<th>Player</th>
<th>Kills</th>
<th>Deaths</th>
<th>KDR</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['KDR'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
We have this up so far: http://gexgaming.com/warstats/index.php
Change the query from
SELECT player, kills, deaths, KDR FROM war_kills ORDER BY kills DESC`
to
SELECT player, kills, deaths, kills / deaths as KDR FROM war_kills ORDER BY kills DESC`
Either modify the SQL query as suggested or do it within the PHP loop:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['player'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . ($row['kills'] / $row['deaths']) . "</td>"; // <--- this one
echo "</tr>";
}
It looks like you need to group your results based on the player name. Also, you need to account for when deaths = 0. Try changing your query to -
SELECT
player,
SUM(kills) as kills,
SUM(deaths) as deaths,
CASE WHEN SUM(deaths) = 0 THEN SUM(kills)
ELSE SUM(kills)/SUM(deaths)
END as `KDR`
FROM war_kills
GROUP BY player
ORDER BY kills DESC
The other answers I read don't account for the fact that someone might have zero deaths. If they do, you'll get a divide by zero error.
inside your while loop,
if ($row['deaths'] == 0)
$kdr = '-';
else
$kdr = $row['kills']/$row['deaths'];
then change $row['KDR'] to $kdr
I have two tables.
visitors_details, with id,scanner_id,time columns
and visitors_info with scanner_id, name,surname columns
I want to get back
id,name,surname,time in a table
i have written this but is not working
$result = mysql_query("SELECT visitors_details.id AS id,
visitors_info.name AS name, visitors_info.surname AS surname, visitors_details.time
AS time FROM visitors_details AS d LEFT JOIN visitors_info AS i ON
d.scanner_id=i.scanner_id ");
echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>surname</th>
<th>Time</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "</tr>";
}
echo "</table>";
any ideas??
Its better to enable some debugging for your code like this:
<?php
error_reporting(E_ALL);
$sql = "
SELECT d.id AS id, i.name AS name, i.surname AS surname, d.time AS time
FROM visitors_details AS d
LEFT JOIN visitors_info AS i ON d.scanner_id=i.scanner_id
";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
try this query
$result = mysql_query("SELECT d.id , i.name , i.surname , d.time
FROM visitors_details AS d LEFT JOIN visitors_info AS i
ON d.scanner_id=i.scanner_id ");
Add this to catch errors. saves a lot of time:
if(!$result) {
echo mysql_error();
}
here im getting problem with my join query..i dont know where is the problem whether my query is wrong or whatelse but the error its giving is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\project\teacher\courses-list.php on line 46
heres my code please mend the code i cudnt find the problem.. :(
courses-list.php
<?php
if ($_SESSION["isteacher"])
{
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
echo "<table border='1'> <br />
<tr>
<th>ID:</th>
<th>Course Name</th>
<th>Description</th>
<th>Subject-ID</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>";
while($row = mysql_fetch_array($result)) // this is the error line
{
echo "<tr>";
echo "<td>" . $row['cid'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['sname'] . "</td>";
echo "<td><a href='courses-edit.php?id=" . $row['id']."'>EDIT</a></td>";
echo "<td><a href='courses-delete.php?id=" . $row['id']."'>DELETE</a></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
here is the error
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id==subjects.id)");
should be
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.description, courses.subjects-id, subjects.id AS sid, subjects.subjectname AS sname FROM courses, subjects WHERE (courses.subjects-id=subjects.id)");
the error portion is
subjects WHERE (courses.subjects-id==subjects.id)");
here is the error ---------^^--------sould be =
also please avoid even dont use the mysql_* even the php manual show the message about that use the mysqli or PDO