echo random id numbers from mysql database without repeating numbers? - php

How do I echo random id numbers from mysql database without repeating numbers?
this is my sample code:
$query = mysql_query("SELECT * FROM store");
$number=mysql_num_rows($query);
for ($count=1; $count<= $number ; $count++)
{
$id = mysql_query ("SELECT id FROM store ORDER BY RAND() LIMIT $number");
$id = mysql_fetch_assoc($id);
$id = $id['id'];
echo $id;
}
It will echo six random numbers but have instances like "1 1 3 2 4 5" where 1 is echoed twice instead of once. thanks in advance

Just order your results by rand and limit their number, your id has to be unique :
SELECT * FROM store ORDER BY RAND() LIMIT 0,6

The Problem is, that you do a SELECT inside of the loop, instead of selecting once and loop over the result.
$query = mysql_query("SELECT * FROM store");
$number=mysql_num_rows($query);
$result = mysql_query ("SELECT id FROM store ORDER BY RAND() LIMIT $number");
while ($row = mysql_fetch_assoc($result)) {
echo $row["id"];
}
BTW: SELECT * to get the number of recordsets is ugly, use SELECT count(id)instead

If you're coming out of php you're probably better off (faster, easier, no locking issues) to randomize your numbers there. And SQL queries inside loops is an antipattern.

Related

PHP mysql data not retrieving from last id

I'm developing one API in php to display data on android app from my database using JSON.
In my app I want to display 20 records first, after display again 20 records once user scroll to top.
I'm requesting the last id of the record from app to show next 20 records from last id.
Here is my code
<?php
$last_movie = 0;
$genre = $_REQUEST['genre'];
$last_movie = $_REQUEST['lastid'];
require_once("connect.php");
$myArray = array();
if($last_movie == 0)
{
$result = $conn->query("SELECT * FROM my_movies WHERE genre = '$genre' ORDER BY year DESC LIMIT 20");
}
else
{
$result = $conn->query("SELECT * FROM my_movies WHERE genre = '$genre' ORDER BY year LIMIT ".$last_movie.",20");
}
if ($result) {
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
$result->close();
$conn->close();
?>
I'm getting values in some genres, but sometimes it show empty JSON.
I tried with this url
http://freemodedapk.com/bobmovies/by_genre.php?genre=Action
its working , whenever I try from last id
http://freemodedapk.com/bobmovies/by_genre.php?genre=Action&lastid=4714
It returns empty JSON. I have values in database.
But some genres working fine
http://freemodedapk.com/bobmovies/by_genre.php?genre=Drama
http://freemodedapk.com/bobmovies/by_genre.php?genre=Drama&lastid=865
I have total 4858 records in the database with all genres.
Anybody can help me to fix empty JSON problems in some of genres ?
Your main issue is in the wrong LIMIT usage: when you utilize 2 parameters for LIMIT keyword, the first one is for OFFSET value (not for IDs), the second one is to limit your result.
In short words: you should use LIMIT 0,20 to get the first 20 results, LIMIT 20,20 to show next 20 results and so on.
Also, your code is insecure - you have SQL injection. Try to not post direct urls on your sites with the source code which includes injection because some bad guys may drop your database or do some other harmful things.
Sample code is listed below (minor changes may be required):
<?php
require_once('connect.php');
$response = [];
$items_per_page = 20;
$page = (int) (($_REQUEST['page'] - 1) * $items_per_page);
$genre = $conn->escape_string($genre); # replace escape_string with proper method if necessary
$result = $conn->query("SELECT * FROM my_movies WHERE genre = '$genre' ORDER BY year DESC LIMIT $page,$items_per_page");
if ($result)
{
$response = $conn->fetch_all($result, MYSQLI_ASSOC); # replace fetch_all with proper method if necessary
}
echo json_encode($response);
$result->close();
$conn->close();
if you want to get last ID to ASC then use to
SELECT * FROM my_movies WHERE genre = '$genre' id<".$last_movie." ORDER BY year LIMIT 0,20
or if you want to get for pagination then your OFFSET value wrong,
you should be use LIMIT 0,20 to get the first 20 results, LIMIT 20,20 to next 20 , LIMIT 40,20
please check your SQL injection
look like Code
require_once('connect.php');
$result = [];
$limit = 20;
$pageNumber = (int) (($_REQUEST['pageNumber'] - 1) * $limit);
$genre = $conn->escape_string($genre);
$getDta = $conn->query("SELECT id,title,stream,trailer,directors,actors,quality,year,genre,length,translation,rating,description,poster FROM my_movies WHERE genre = '".$genre."' ORDER BY year DESC LIMIT $pageNumber,$limit");
if ($result)
$result =$conn->fetch_all($result, MYSQLI_ASSOC);
echo json_encode($result);
$result->close();
$conn->close();

php mysql random rows no duplicates

Hi im trying to get 5 random rows from a database and then display them. i currently do this but it does result in duplicates.
i need to change the limit to 5 and then store them in an array but how do i do that? or is there a better way?
function GetPlayer($link){
if (isset($_SESSION['username'])) {
$x = 0;
while($x <= 5) {
$sql = "SELECT * FROM userstats ORDER BY RAND() LIMIT 1; ";
$result = mysqli_query($link,$sql);
$row = mysqli_fetch_assoc($result);
if($row['username'] !== $_SESSION['username']){//add so it dosent put duplicates
echo ("<tr>");
echo ("<th>".$row['username']." </th>");
echo ("<th>Level: ".$row['Level']." </th>");
echo ("<th>Player Stats:".$row['Attack']."/".$row['Defence']." </th>");
echo ("<th>Win Chance: ");
echo CalculateWinChance($link,$row['Defence']);
echo ("<th><input type ='submit' name = 'Attack_Btn' value ='Attack'></th>");
echo ("</tr>");
$x++;
}
}
}
}
Why dont't you try to request 5 results (LIMIT 5) AND loop this? It will no return any duplicates. Four queries less would be a side effect.
$sql = "SELECT * FROM userstats ORDER BY RAND() LIMIT 5; ";
while($row = mysqli_fetch_assoc($result)){
...
}
Instead of calling the query 5 times within the loop, you should have a single query. That's the most optimal approach.
You can use GROUP BY clause to select unique rows.
Your query should be like the following:
SELECT *
FROM userstats
GROUP BY username
ORDER BY RAND()
LIMIT 5
You'll want to sanitize the inputs in the query and/or use a prepared statement, but this should get you pretty close to what you want:
$sql = 'SELECT * FROM userstats WHERE username != ? GROUP BY username ORDER BY RAND() LIMIT 5';
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $_SESSION['username']);

How can I make sure that two rows selected at random are different from one another?

I have a table photos with many photos in it and I need to select two at random:
In getnew.php
$result = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 1");
$result2 = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 1");
$row = $result->fetch_assoc();
$img1link = $row['link'];
// more stuff from $row
$row2 = $result2->fetch_assoc();
$img2link = $row2['link'];
// more stuff from $row2
However I need to prevent it from selecting the same photo twice (the selected photos must be different), i.e. $img1link should not = $img2link. I then need to retrieve the data using $.getJSON in another file, using an array at the end of getnew.php.
The array at the end of getnew.php:
echo json_encode(array('img1'=>$img1link,'img2'=>$img2link, ...(etc)... ));
How can I make sure the selected photos are different by the time the variable is stored in the array? I tried to create an if/else statement but didn't really understand what I was doing.
You can just execute once but get two instead so that you'll never pick the same row:
$result = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 2");
$row = $result->fetch_assoc();
$row2 = $result->fetch_assoc();
// invoke `->fetch` twice to get the first and second row
$img1link = $row['link'];
$img2link = $row2['link'];
Sidenote: Be careful of that ORDER BY rand() clause since it'll be slow on large data sets. You can use an alternative with #Bill Karwin's great answer
Run a single query -
$result1 = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 2");
while($row = $result->fetch_assoc()) {
$imglink[] = $row['link'];
}
You will get the links in $imglink[] array.

select random unique records from mysql db using php

i am trying to fetch random records from mysql database , which it worked but i need the records to be also unique when i fetch them as they duplicated on the output, here is my code:
<?
for ($counter = 1; $counter <=5;$counter++)
{
$randomPostSelect = mysql_query("SELECT DISTINCT * FROM beventreservation WHERE (beventStatus='online' OR beventStatus='soldout') ORDER BY RAND() LIMIT 5") or die(mysql_error());
$fetchPosts = mysql_fetch_array($randomPostSelect) or die(mysql_error());
echo '<li>'.$fetchPosts['eventTitle'].'</li>';
echo '</br>';
}
?>
how can i do that ?
You have all the code in a for loop including the code that runs the query; you "extract" 5 rows from db but only fetch the first, and repeat this 5 times. Instead you should run the query once, then loop on your results until you reach the end of the results:
<?
$randomPostSelect = mysql_query("SELECT DISTINCT * FROM beventreservation
WHERE (beventStatus='online' OR beventStatus='soldout') ORDER BY RAND() LIMIT 5")
or die(mysql_error());
while ($fetchPosts = mysql_fetch_array($randomPostSelect))
{
echo '<li><a href="reservation.php?rev='.$fetchPosts['eventId'].'">'.$fetchPosts['eventTitle']
.'</a </li>';
echo '</br>';
}
?>

php mySQL, retrieve one row by number, store values as assoc.array

Pretty new to all this, so if I am going about my puzzle in a crazy way please tell me!
I have a table in a mySQL database with the columns title, hyperlink, imagelink
I want the php to select a row at random, then be able to save the title, hyperlink and imagelink as php variables. (so I can then output them into html)
so I could have for e.g
psuedoey code to get the idea
chosenNumber = randomNumber();
$chosenTitle = title[chosenNumber]
$chosenHyperlink = hyperlink[chosenNumber]
$chosenImagelink = imagelink[chosenNumber]
echo "<a href = $chosenTitle><img src= $chosenImagelink/> $chosenTitle </a>";
I think I need to use an assoc array like this here but I am very confused, because I looked through the various php mySQL fetch_assoc fetch_row etc and can't find which one to do what i need :(
What I have so far is
// database table name information
$number = "number";
$title = "title";
$hyperlink = "hyperlink";
$imagelink = "imagelink";
// sql to select all rows of adverts
$sql = "SELECT $number, $title, $hyperlink, $imagelink FROM $table";
//execute the sql
$data = mysql_query($sql, $link);
//count the number of rows in the table
$bannerCount = mysql_num_rows($data);
//generate a random number between 0 and the number of rows
$randomNumber = mt_rand(0, $bannerCount); //do I need to do bannerCount-1 or similar here?
$chosenNumber = $randomNumber;
//select data from a random row
First time post, be kind please, and thanks for any replies or explanations!
Using ORDER BY RAND() LIMIT 1 is a decent way to get a single row out of a smaller table. The downside to using this method is that RAND() must be calculated for every row in the table, and then a sort must be performed on this non-indexed value to calculate the row you want. As your table grows, ORDER BY RAND() becomes horribly inefficient. The better way to handle this is to first get a count of the number of rows in the table, calculate a random row number to read, and use the LIMIT [offest,] count option on your SQL query:
$sql = "SELECT COUNT(*) as rows FROM $table"
$res = mysql_query($sql);
if (!$row = mysql_fetch_assoc($res)) {
die('Error Checking Rows: '.mysql_error());
}
$rows = $row['rows'];
// now that we know how many rows we have, lets choose a random one:
$rownum = mt_rand(0, $rows-1);
$sql = "SELECT number, title, hyperlink, imagelink FROM $table LIMIT $rownum,1";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
// $row['number'] $row['title'] etc should be your "chosen" row
This first query asks the SQL Server how many rows are available, then LIMITs the result set of the actual query to only return 1 row, starting at the random number row we picked.
I think if you use the "limit" clause, it would be quite efficient:
SELECT number, title, hyperlink, imagelink FROM $table order by rand() limit 1
Why do you set up variables for the table name and field names?
If you want to get records randomly, you can simply modify your sql query and each time you will get random ordering of records, just add order by rand() to your query and limit clause if you want to get just one random record:
$sql = "SELECT $number, $title, $hyperlink, $imagelink FROM $table
order by rand() limit 1";
Once you have done that, you need to use functions like mysql_fetch_array or mysql_fetch_object to get the rows actually:
$data = mysql_query($sql, $link);
while ($row = mysql_fetch_array($data))
{
echo $row['title'] . '<br />';
echo $row['hyperlink'] . '<br />';
echo $row['imagelink'] . '<br />';
}
With:
$row = mysql_fetch_array($data)
You have $row array available at your disposal to echo values at any place of your page like:
echo $row['title'];
echo $row['hyperlink'];
echo $row['imagelink'];

Categories