$sql = "SELECT `url`,`title`,`vid` FROM `video` ORDER BY `time` DESC limit 15";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
This SQL can select the top new 15 rows data.
I want display the top new 100, but just show 15
How to select faster?
$sql = "SELECT `url`,`title`,`vid` FROM `video` ORDER BY `time` DESC limit 100";
i assume that $row[0] => gives first record.
--
function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
return array_slice($numbers, 0, $quantity);
}
--
foreach (UniqueRandomNumbersWithinRange(0,100,15) as $row_number)
{
$content=$row[$row_number];
echo $content['title'];
}
Well you could try fixing it like:
<?php
//get the max count for the table;
$max="SELECT id FROM video order by time desc LIMIT 1";
$start="SELECT id FROM video order by time desc LIMIT 100, 1";
$page_size=15;
$rand_no=rand(start,$max - page_size);
$result_set="SELECT * FROM video order by time LIMIT $rand_no,page_size";
NB: it's an abstract code explains the logic.
Related
I have the following code which selects information from one random row.
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 1 ");
while($rows = mysql_fetch_array($query))
{
$question = $rows['question'];
$hint = $rows['hint'];
$level = $rows['level'];
$keyword = $rows['keyword'];
$showme = $rows['showme'];
$picture_path = $rows['picture_path'];
}
This works well for me but I now I need to be able to select two more DIFFERENT pictures from the picture_path column and assign them to variables. Again, all three rows need to be different.
Any tips for a newbie on how to do this?
Just change your query as follows:
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
You are ordering by ORD() so it will give you different records.
No, new modification, just change the limit to 3 (whatever your need).
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
As your are already getting random values with order by clause it will always return different values so you just need to edit your limit value and you are done!
$query = mysql_query("SELECT * FROM lines_angles_shapes ORDER BY RAND() LIMIT 3");
I am trying to run 2 database querys the second using an array from the first, any one have any ideas why this stops the page from loading?
$query = $this->db->query("SELECT category_id FROM listings WHERE listing_id = '1' LIMIT 1");
foreach ($query->result() as $row)
{
$query2 = $this->db->query("SELECT listing_title FROM listings WHERE listing_type = '".$row->category_id."' ORDER BY RAND() LIMIT 2");
foreach ($query2->result() as $row)
{
echo $row->listing_title;
}
}
You are overwriting the outer $row in your inner loop
$query = $this->db->query("SELECT category_id FROM listings WHERE listing_id = '1' LIMIT 1");
foreach ($query->result() as $row)
{
$query2 = $this->db->query("SELECT listing_title FROM listings WHERE listing_type = '".$row->category_id."' ORDER BY RAND() LIMIT 2");
foreach ($query2->result() as $row2) // <--- $row2 not $row
{
echo $row2->listing_title;
}
}
Try this:
(Take advantage of prepare statements, the prepare() function, etc)
$query = "SELECT category_id FROM listings WHERE listing_id = '1' LIMIT 1;";
$query2 = "SELECT listing_title FROM listings WHERE listing_type = :categoryId ORDER BY RAND() LIMIT 2;";
$db = $this->db;
$statement = $db->prepare($query);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
while($row = $statement->fetch())
{
$categoryId = $row['category_id'];
$statement2 = $db->prepare($query2);
$statement2->execute(array(':categoryId' => $categoryId));
$statement2->setFetchMode(PDO::FETCH_ASSOC);
while($row2 = $statement2->fetch())
{
$listingTitle = $row2['listing_title'];
echo $listingTitle;
}
}
This is not very good code. Why use a foreach loop when the Select query has Limit 1?
Since you are going for a category id where listing_id=1 (with LIMIT 1), your code could just be shortened to:
SELECT listing_title FROM listings WHERE listing_id=1 ORDER BY RAND() LIMIT 2
Also, ORDER BY RAND() is a big resource killer on large tables. I recommend finding a more proper way to order your results.
EDIT: Full code that I would use:
$db=$this->db; //Just so we don't have to keep referencing $this (assuming you are not in a class)
$sql="SELECT listing_title
FROM listings
WHERE listing_id=?
ORDER BY RAND()
LIMIT 2";
$statement=$db->prepare($sql);
$statement->execute(array(1));
while($result=$statement->fetchObject()){
echo $result->listing_title;
}
Pseudocode
$res = Query("SELECT * FROM `table` ORDER BY `date` DESC LIMIT 15");
SortArray(&$res, 'date', 'asc');
If describe in words, then take the last part of the data is sorted in descending order from the database, but to give the data sorted in ascending order.
Try:
$res = Query("SELECT * FROM ( SELECT * FROM `table` ORDER BY `date` DESC LIMIT 15) ORDER BY `date` ASC");
You can use a usort function to sort the array by specific key:
$res_array=array();
while($row=mysql_fetch_row($res))
$res_array[]=$row;
$new_arr = usort($res_array,"my_func");
function my_func($a, $b)
{
if ($a['date'] == $b['date']) {
return 0;
}
return ($a['date'] < $b['date']) ? -1 : 1;
}
*may need some debugging. take the idea.
Instead of making some magical SQL query, you should select 15 first rows in descending order. And then just read the results from the end.
$statement = $pdo->query('SELECT * FROM `table` ORDER BY `date` DESC LIMIT 15');
if ( ! $statement->execute())
{
throw new Exception('query failed !');
}
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
while ( $row = array_pop($data))
{
var_dump( $row );
}
How can i select the first 5 results and then add a see more option?
Below is the current code:
<?php
$query="SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "";
$i=0;
while ($i < $num) {
$otheris=mysql_result($result,$i,"sender_full_name");
$sysid=mysql_result($result,$i,"sender_id");
$dob=mysql_result($result,$i,"dob");
// If $dob is empty
if (empty($dob)) {
$dob = "No new messages -
<a id=missingdob href=/test.php?id=$uid>
<bold>check later</bold></a>";
}
echo "<br><div id=linkcontain>
<a id=otherlink href=$mem/profile.php?id=$uid>
$manitis</a>
<br><div id=dobpres>$dob</div></div>";
echo "";
$i++;
}
?>
You should try to select 6 rows first time and if you get 6 records then show first 5 with a "show more option"
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 0, 6";
For subsequent times you should have your query like this:
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 6, 5";
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 11, 5";
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 16, 5";
...
...
And every time "show more option" if you are able to fetch requested number of records.
$query="SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 5";
http://dev.mysql.com/doc/refman/5.5/en/select.html
you could consider LIMIT 6 display only up to 5 if 6th exist display there is more options...
window.onload = function(){
$(".box").hide();
$(".box").slice(0, 5).show(); // select the first ten
$("#load").click(function(e){ // click event for load more
e.preventDefault();
$("div:hidden").slice(0, -1).show(); // select next hidden divs and show them
$("#load-more-div").html(' ');
});}
how do I order this result??
$range = 5; // you'll be selecting around this range.
$min = $rank - $range;
$max = $rank + $range;
$limit = 10; // max number of results you want.
$result = mysql_query("select * from table where rank between $min and $max limit $limit");
while($row = mysql_fetch_array($result))
{
echo $row['name']." - ".$row['rank']."<br>";
}
$result = mysql_query(
"select * from table where rank between $min and $max " .
"order by rank asc limit $limit"
);
Use the "order by"- clause:
mysql_query("select * from table where rank between $min and $max order by rank limit $limit");
This will order your result from little to big values.
Use "order by rank desc" to order in descendent direction. (big -> little)