SQL Server ORDER BY Not working in PHP - php

$query=mssql_query ('SELECT USER_INDEX_ID FROM T_o2jam_login');
echo "<table border =\"0\" style=\"color: gray;\" cellspacing=\"0\" cellpadding=\"0\" CLASS='boldtable'><tr><th colspan=\"9\">Online Players</th></tr><tr><td>Level </td> <td> Nick </td> </tr>";
if (mssql_num_rows($query)) {
while ($row = mssql_fetch_array($query)) {
$q2 = mssql_query ("select * from t_o2jam_charinfo where USER_INDEX_ID=$row[USER_INDEX_ID] ORDER BY Level DESC");
$nt=mssql_fetch_array($q2);
echo "<tr><td>Lv. $nt[Level] </td><td> $nt[USER_NICKNAME] </td></tr>" ;
I am trying to sort online users by Level on Descending order. From 99 to Level 1. It displays the data but they are not sorted. What's the problem right there? Thank you!

You need to have the order by in your first query. Your second query is within a loop, so it's going to first go by the order of the first one.
This really should be one query with a JOIN.
SELECT charinfo.*, FROM t_o2jam_charinfo charinfo
INNER JOIN T_o2jam_login login
ON charinfo.USER_INDEX_ID = login.USER_INDEX_ID
ORDER BY `Level` DESC
But since you never use any of the info from the "login" table, one wonders why it's even used at all.
Anyway, never run queries in loops.

Related

mysql group the sum(column) to maximum values but the sums must be below a given value

I have a table with columns name and amount, i want to query data that sum(amount) upto lets say 1000 in each group, in cases where a row value is greater than 1000 it can be still be displayed in its own group. i have tried this using a php loop of sum that range between 500 to 1000 but this way produces so many groups that i wish could be a voided. is there any way that we can do this in a single mysql query. regards
this is the way i crooked my way!
//this query helped me in maneuvering in provision of limits each time
$totalLines=mysql_query("select COUNT(*) as totalRows from chasis where chasis.BL_No =xxxx order by chasis.BL_No ")or die(mysql_error());
$fetch_totalLines=mysql_fetch_array($totalLines);
$found_rows=$fetch_totalLines['totalRows'];
$total=0;
$Toplimit=0;
$z=0;
a:
if($Toplimit>0){
$Toplimit=$Toplimit;
//$found_rows=$found_rows-$Toplimit;
$total=0;
}
$query=mysql_query("select * from chasis where chasis.BL_No =xxxx order by chasis.duty_amount limit ".$Toplimit.",".$found_rows." ")or die(mysql_error());
$i=1;
while($row=mysql_fetch_array($query)){
$total=round($row['duty_amount'],0)+$total;
echo'<tr>
<td>'.$i.'</td>
<td>'.$row['BL_No'].'</td>
<td>'. mb_substr($row["make"], 0, 1,"UTF8")."-";
echo $row["model"];
echo'</td>
<td>'.$row['chasisNo'].'</td>
<td>'.$row['cc'].'</td>
<td>'.date("Y F", strtotime($row['year'])).'</td>
<td>'.$row['EntryNumber'].'</td>
<td>'.round($row['duty_amount'],0).'</td>
<td> </td>
</tr>';
if($total<=1000 and $total>=500 ){
echo"<td colspan='8' align='right'><b>Duty Cheque for ".$i." units</b></td>
<td><b>".$total."</b></td>
";
$Toplimit=$Toplimit+$i;
$z=$z+$i;
goto a;
}
$i++;
}
I have decided to show everything that i did may to reflect the usage of goTo statement to hold the column id
I know this is a poor way to handle this but am really stuck.
Could you please try this way
select * from chasis where chasis.BL_No =xxxx AND chasis.duty_amount>=500 AND chasis.duty_amount<=1000 order by chasis.duty_amount

Just display the one result PHP

I have this code which display a list of players and their points in a contest:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$result=mysql_query("SELECT * FROM `pointstable` WHERE `contestfield` = 1 ORDER BY `pointsfield` ASC");
while ($info=mysql_fetch_assoc($result)) {
$playerid=$info['playeridfield'];
$playername=$info['playernamefield'];
$pointsfield=$info['pointsfield'];
?>
<tr><td style="text-align:center; padding-bottom:5px" valign="middle"><?=$playername?> made <?=number_format($pointsfield)?></td></tr>
<?php } ?>
</table>
This will display the complete list, but when a user plays more than one time, it displays all the points he has...
So what I want is to display only the first value (in this case, the lowest points since the objetive of the contest is to get the lowest score) The playeridfield is a unique number for each player, each player has one.
Who can this be done?
Ad this to your SQL query at the end LIMIT 1
Just use the mysql min function and group by id:
SELECT playeridfield, playernamefield, MIN(pointsfield) AS pointsfield FROM `pointstable`
WHERE `contestfield` = 1 GROUP BY `playeridfield` ORDER BY `pointsfield` ASC
Try changing your SQL query to this:
$result=mysql_query("SELECT * FROM `pointstable` WHERE `contestfield` = 1 ORDER BY `pointsfield` ASC LIMIT 1");
The LIMIT 1 should make it so only one row is returned from the query.

how to select data from 2 table of databace in mysql....?

I have 2 table of date , student_info and student_payment in my databace...
in student_info i have:
id, student_id,student_mail,student_pass,student_name,...
and in student_payment have:
id,student_id,student_payment_id,student_payment_date,...
so my problem is here, i wanna select student_name where student_id form student_info but i have problem and mysql give my an error:
$db->connect();
$sql = "SELECT * FROM `student_payment`";
$rows = $db->fetch_all_array($sql);
$student_id = $rows['student_id'];
$sql2 = "SELECT * FROM `student_info` WHERE student_id=$student_id";
$rows2 = $db->fetch_all_array($sql2);
$db->close();
foreach($rows as $record ){
// i wanna to use student_name in first line
echo "\n<tr>
<td>$record[student_id]</td>
<td dir=\"ltr\">$record[student_payment]</td>
<td dir=\"ltr\">$record[student_payment_id]</td>
<td dir=\"ltr\">$record[student_payment_bank]</td>
<td dir=\"ltr\">$record[student_payment_type]</td>
<td dir=\"ltr\">$record[student_payment_date]</td>
<td dir=\"ltr\"></td>
</tr>\n";
}
but i dont know how to connect student_id and student_name and use in foreach because i have 2 rows of data.
(i'm a beginner in PHP / MySql)
Instead of querying database twice, you can instead join the tables to get the rows you want. Try to execute the query below in PhpMyAdmin or directly on MySQL Browser.
SELECT a.*, b.*
FROM student_info a
INNER JOIN student_payment b
ON a.student_ID = b.student_ID
-- WHERE ...if you have extra conditions...
ORDER BY b.student_payment_date DESC
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
It is possible to fix it with INNER JOIN, you can join 2 tables and use both values from 1 query.
http://www.w3schools.com/sql/sql_join_inner.asp
Or you can use the OOP way, not sure if that is what you need.
Make 2 objects from the 2 query's and put them in a foreach.
try this
$sql2 = " SELECT * FROM `student_info` WHERE student_id= '$student_id' ";
try this
$sql2 = "SELECT * FROM `student_info` WHERE student_id IN ($student_id)";
foreach($rows as $record ){
// i wanna to use student_name in first line
echo "\n<tr>
<td>$record[student_id]</td>
<td dir=\"ltr\">".$record['student_payment']."</td>
<td dir=\"ltr\">".$record['student_payment_id']."</td>
<td dir=\"ltr\">".$record['student_payment_bank']."</td>
<td dir=\"ltr\">".$record['student_payment_type']."</td>
<td dir=\"ltr\">".$record['student_payment_date']."</td>
<td dir=\"ltr\"></td>
</tr>\n";
}
Use Mahmoud Gamal code
But always select the needed columns only not all because
In future number of columns in table may increase which may decrease the performance of your application.
Also it may contain some important information not to be leaked.
It seems like you want a report of all payments made. The student name is to be displayed with the payment information. The results will probably have more than one payment per student.
This result is ordered by student name, and then payment date (most recent first)
SELECT s.student_name, sp.*
FROM student_payment sp
INNER JOIN student_info s ON s.student_ID=sp.student_ID
ORDER BY s.student_name ASC, sp.student_payment_date DESC
try to join the table and use single query instead of two -
$sql = "SELECT * FROM student_info, student_payment
WHERE student_info.student_id=student_payment.student_id"

PHP/MySQL where/order clause not ordering data

I am trying to order data from the table 'tech_inquiry' by the Field 'number' in descending order. This should put the results in order by year. Each 'number' field corresponds to another field with a title/date (what is actually viewed by visitors) which I can't sort by because the date is at the end of the title and not always in the same place.
Also, the table 'tech_inquiry_allowed' determines what is viewable to who when logged in.
With that said, here is the code:
<?
$query2=mysql_query("SELECT * FROM tech_inquiry_allowed where term_code = '$term_code' ");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$query3=mysql_query("SELECT * FROM tech_inquiry WHERE id= '$id2' ORDER BY number DESC");
$row3=mysql_fetch_assoc($query3);
$name3=$row3['name'];
?>
<hr />
<li><? echo $name3; ?> </li>
<?
}
?>
Also, I have another 'admin' section that is able to order data correctly. The only difference is there is no conditional 'where' clause because no user authentication is needed (it is used to add data to the table).
Here is that code:
<?
$query2= mysql_query("SELECT * from tech_inquiry ORDER BY number DESC");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$name2=$row2['name'];
?>
<hr />
<li><? echo $name2; ?> </li>
<?
I am wondering if it might be the fact that we are running a query inside a loop.
There are a number of problems here. First of all you only need one query to accomplish this. Please read up on SQL query writing when you get a moment. You will find it to be VERY helpful.
Second, you are using way more code than you need to. Below is the much simplified, cleaner, and probably faster code.
<?php
$query = mysql_query("SELECT * FROM tech_inquiry ti WHERE id IN (SELECT id FROM tech_inquiry_allowed where term_code = '$term_code') ORDER BY ti.number");
while ($row = mysql_fetch_object($query)) {
echo "<hr />\n<li><a href='get_file.php?id={$row->id}'>{$row->name}</a></li>";
}
?>
You are not looping the inner query. Anyway, you should be using a single query for this:
SELECT allowed.*, inquiry.*
FROM tech_inquiry_allowed allowed
INNER JOIN tech_inquiry inquiry
ON inquiry.id = allowed.id
WHERE term_code = '$term_code'
ORDER BY inquiry.number DESC

Using while() display even with NULLS

I have a table and I am displaying its contents using PHP and a while(); I have about three fields in the table that are NULL but can be change, but I want them to still display all the results in my table.
But, it only shows the records with data in EVERY field. Anyone how I can display it? I get a count of the table and it gives me 2, but only displays one.
<h3>Viewing All Updates</h3>
<h4>Below are all active updates for COTC</h4>
<table>
<thead>
<tr>
<th>Site Name</th>
<th>Page</th>
<th>Flag</th>
<th>Date Sent</th>
<th>View</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT sname,page_name,date_submitted,u_id,clients.c_id,flag,completed FROM updates INNER JOIN clients ON updates.c_id = clients.c_id INNER JOIN pages ON updates.page = pages.p_id ORDER BY date_submitted DESC";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query)){
$completed = $row['completed'];
if($completed == 1){
print '<tr class="quiet">';
}else{
print '<tr>';
}
print '<td>'.$row['sname'].'</td>';
print '<td>'.$row['page_name'].'</td>';
print '<td>'.$row['flag'].'</td>';
print '<td>'.$row['date_submitted'].'</td>';
print '<td class="center"><img src="images/page_edit.png" alt="Edit entry!" /></td>';
print '</tr>';
}
?>
</tbody>
</table>
Your PHP is correctly printing every row returned. I believe your problem is in the query.
SELECT sname,page_name,date_submitted,u_id,clients.c_id,flag,completed
FROM updates INNER JOIN clients ON updates.c_id = clients.c_id
INNER JOIN pages ON updates.page = pages.p_id
ORDER BY date_submitted DESC
This query will only return a row in updates if it has a matching one in clients and a matching one in pages. If you want the clients or the pages joins to be optional (a updates row that has c_id or page of NULL will still return) change them from INNER JOINs to LEFT JOINs.

Categories