I'm writing a simple message board where you can reply to any thread and then reply to any reply and so on.... everything works well but is there a simple method to loop the query as this could potentiality go on and on and on
$rsql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC";
$result = runSQL($rsql);
while ($row = mysql_fetch_array($result)) {
echo "".$row[title]."";
$rsql2 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID]' ORDER BY dateTime DESC";
$result2 = runSQL($rsql2);
while ($row2 = mysql_fetch_array($result2)) {
echo "".$row2[title]."";
$rsql3 = "SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";
$result3 = runSQL($rsql2);
while ($row3 = mysql_fetch_array($result3)) {
echo "".$row3[title]."";
}
mysql_free_result($result3);
}
mysql_free_result($result2);
}
mysql_free_result($result);
You could try UNION, probably not the best solution but it will work
$sql = "SELECT * FROM rotd_mb WHERE reply='N' ORDER BY dateTime DESC
UNION
SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row[messageID] ORDER BY dateTime DESC
UNION
SELECT * FROM rotd_mb WHERE reply='Y' AND replyID='$row2[messageID]' ORDER BY dateTime DESC";
A better idea would be to re-design your database table structure.
Related
I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";
i have a question which is my limit statement didn't work i want the content select from database and limit the show content in 40 only but it didn't work
here is my SQL statement with php code
$chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];
$sql="SELECT * FROM chatroom_chat WHERE chatroom_id ='$chatroomID'";
$result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result1)) {
$chat = $row['chat_id'];
$sql3 ="SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 0,40
) sub
ORDER BY id ASC ";
$getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));
/*here have statement to get username*/
while($row3 = mysqli_fetch_array($getChatData)) {
echo "<div>all content</div>";
}
}
does my code have any syntax error? i no sure why it didn't work
SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 40
) sub
ORDER BY id ASC
My calendar table has start and end column and they have the same data type which is date. Now I'm having trouble on how to query and select the data in desc order according to the date recorded in the start column. Does anyone know how does my query should be?
I only have this:
//code indentation
$query = "select * from calendar";
$res = mysqli_query($conn,$query);
while($row=mysqli_fetch_array($res))
{
...
}
EDIT:
The right query is
$query = "select * from calendar ORDER by start DESC";
Thanks to everyone who answered! :)
Does just doing a simple ORDER BY not work?
$query = "select * from calendar ORDER BY start DESC";
$query = "select * from calendar ORDER BY start DESC";
$res = mysqli_query($conn, $query);
while( $row=mysqli_fetch_array($res) )
{
// your code
}
I think this should work. You can use ORDER BY.
$query = "select * from calendar ORDER BY date1, date2 DESC";
Mysql will first check for date1 if it is blank it will go for date2
I was able to apply this line onto phpMyAdmin and it worked just fine.
SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1
The problem is that when I added the rest of the code, the recent date shows up blank on the webpage. Am I missing something in this code?
<?php
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
echo "$date";
?>
Any help is appreciated. Thank you.
. Try this
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY. date DESC LIMIT 1";
$result = mysql_query($query);
$r = mysql_fetch_assoc($result);
$date = $r['date'];
echo "$date";
You didn't set $date variable. You need to use mysql_fetch_array function for your $result variable.
Ex:
`
$query = "SELECT id, date_format('date', '%m.%d.%Y') as 'date' FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row); }
`
I'm trying to sort this query by time.
I have a gaming match system. And I want to get a list of next 5 matches from my local time zone.
<?php
include_once "include/dbcompo.php";
$q=mysqli_query($con, "SELECT * FROM kamper ORDER BY tid LIMIT 5");
while($row = mysqli_fetch_array($q))
{
$clan1 = $row['clan1'];
$clan2 = $row['clan2'];
$server = $row['server'];
$tid = $row['tid'];
echo $clan1." ".$clan2." ".$server." ".$tid;
echo "<br />";
}
?>
Add a WHERE clause in your query: WHERE tid > NOW().
With NOW() you take the time of the server, maybe you should replace it with new DateTime(null)->getTimestamp()
Something like that:
<?php
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > NOW() ORDER BY tid LIMIT 5');
// or
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > '.new DateTime(null)->getTimestamp().' ORDER BY tid LIMIT 5');
?>
Two options:
1st option: Make the time field a numeric field and sort in PHP:
$queryResult = mysqli_query($con, $query);
while($row = mysqli_fetch_array($queryResult) {
$oldArray[$row['time'] = $row;
}
$array = ksort($oldArray);
foreach($array as $time=>$row) {
// do something
}
2nd option: Make a subquery
SELECT * FROM (
SELECT * FROM kamper WHERE timezone = 'UTC' ORDER BY tid
) LIMIT 5