Random row php mysql - referencing? - php

I'm new to php, here is my problem:
I want to select a row at random in which the same random value can be referenced later on in the page, i.e embedded in the youtube embed object. I have this working but it changes the value as the random selector is executed again (?)
mysql_connect("====","====","=====")or die(mysql_error());
mysql_select_db("yt")or die(mysql_error());
$result = mysql_query("SELECT * FROM utube ORDER BY RAND() LIMIT 1");
$results = mysql_fetch_assoc($result);
echo $results[id]; /* print youtube id */
echo '<br />'.$results[rating];
Can anyone help me to set up something that lets me reference the row so that I don't have different values for each part of the page where I'm using the random row from my database? Thanks

Save the row into a variable which won't change.
$result = mysql_query("SELECT * FROM utube ORDER BY RAND() LIMIT 1");
$results = mysql_fetch_assoc($result);
$saved_row = $results; //don't change $saved_row later on in the script
echo $saved_row['id']; /* print youtube id */

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();

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.

PHP code to fetch random data from database with link

I have a project from my college. I need to make a site which will generate random data from database. But it will also need to generate a link. So that people can copy that link (as the website is generating different data, people can see the data they want by copying the URL).
I was thinking to use RAND(). But after joining stackoverlow, I see that RAND() is not a good way.
I can fetch random data using RAND() but it is not making any URL. Which is another problem too. I post this problem before. I think I shouldn't use the RAND() function.
this is the code I'm using at this moment:
<!--PHP code for fetching data from database-->
<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xlsx_db", $con);
$result = mysql_query("SELECT * FROM sheet1 ORDER BY RAND() LIMIT 1");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_array($result);
echo "<b>Quote: </b>";
echo $row['quote']."<br>";
echo $row['by']."<br>";
?>
Anyone could please suggest me how to do that? Any kind of help will be very much appreciated.
Not sure if this is actually what you need, but:
<?php
$rand = true;
if (isset($_GET['id'])) {
if (is_int($_GET['id'])) {
$id = (int) $_GET['id'];
$q = 'SELECT * FROM table WHERE id = ?';
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_num_rows($stmt) == 1) {
$rand = false;
// display
}
}
}
if ($rand) {
$q = 'SELECT * FROM table ORDER BY RAND() LIMIT 1';
// run query
$res = mysqli_fetch_array($r);
$url = 'www.example.com/index.php?id=' . $res['id'];
}
?>
The prepared statements aren't strictly necessary, since we already make sure that the $_GET['id'] value is an integer, but for the sake of completeness ;D
If you're selecting the data from multiple tables, you could have another table storing the combinations of data and then generate the url using the combination's ID as id parameter.
If you want to get a permalink, you will need to create another page. It will fetch an ID (or something that is identifying the data record you have chosen randomly) and display the specific data. You link to this at your random page.
Find a random row in your database.
Get the row's id.
Concatenate a URL with the id.
The URL should be pointing to a page that would extract the row with the id provided from the table.
I found this the other day and thought it to be pretty comprehensive:
http://mysql.rjweb.org/doc.php/random

echo random id numbers from mysql database without repeating numbers?

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.

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