PHP -- How to select random but different rows from table - php

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

Related

dynamically encode data in json with limit using php for mobile app like android , ios

I have record of 1000 and more records. I have to provide only 50 records per request like
0-50, 50-100, 100-150 like this. I'm using the following code:
public function get_database($data)
<?php
{
$start = $data['start'];
$limit = $data['limit'];
$alumni_details = array();
$query1 = "select * from alumni where
status='Active',limit '".$start."','".$limit."' ";
$query_run = mysql_query($query1);
while($row = mysql_fetch_assoc($query_run))
{
$row['date_of_birth'] = date('d M, Y', strtotime($row['date_of_birth']));
$alumni_detail['alumni_details'] = $row;
$alumni_details[] = $alumni_detail;
}
echo json_encode($alumni_details);
}
But I need to take only user_id based on that I need to encode data in json dynamically with limit.
Your code as below:
$start = $data['start'];
$query = SELECT * FROM `alumni` WHERE `status` = 'Active' ORDER BY `alumni_id` LIMIT $start,5"
It should be dynamic 5 would not be taken as constant. take it in some variable,
and yes it should be fixed, but as per requirement in future it can be change so take it in variable.
$start=$data['start'];
$query=select * from alumni where status='Active' order by alumni_id Limit $start,5"
where 5 is the limit set it according to requirement
$start=$data['start'];
$query=select * from alumni where status='Active' order by alumni_id Limit $start,5"
You have syntax error in your query string.
Use concatenation for example:
$query1 = "select * from alumni where status='Active' limit ".$start.",".$limit;
Or just put variables into double quoted string:
$query1 = "select * from alumni where status='Active' limit $start, $limit";
There is a typo also: you should use $start variable in the query, not $star

Select random row in SQL where Status = 0 using php

I wonder... is this possible to select a random row in my DB where only the record that contains field->Status = 0 ? because i only need a row that contains 0 in the field name=Status. if the status contains = 1 , then the row will not belong in the randomization.
my code in the query is
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1");<br>
$result = mysql_query( " SELECT * FROM `$haha` ORDER BY RAND() limit 1 Where Status=0");
But it does not work.. i appreciate your replies.. thank you very much!
Try below code:
$result = mysql_query("SELECT * FROM `".$haha."` Where Status=0 ORDER BY RAND() limit 0,1");
Your where clause should be before then the order by clause.
And want to know what is the value of $haha.
You use the wrong syntax. The good one:
$result = mysql_query( " SELECT * FROM `$haha` Where Status=0 ORDER BY RAND() limit 1");

Select a random value from an array after a selection from database

I'm trying to get a random value from an array that is populated with data from the database. I'm selecting some products from the DB and display them on the front page, but i need to display different (random) producs everytime the page is reloaded.
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
$row2 = mysql_fetch_assoc($row);
So, i think that $row2 is now an array, and has all the infos that i selected previously from the database. How do i select a random 'row' from that array now?
Thanks
You are looking for array_rand, see: http://php.net/manual/en/function.array-rand.php
Example:
$rand_key = array_rand($row2);
echo $row2[$rand_key]; // your random
You could also directly select a random row from your DB:
SELECT *
FROM anunt
WHERE lichidareStoc = 0
ORDER BY rand()
LIMIT 1
But be aware, this will reduce your performance, especially on bigger tables.
Side Note: mysql_* function are deprecated, use mysqli_* instead.
If you want to select a random entry, you could do something like this:
$myRows=array();
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
while($row2 = mysql_fetch_assoc($row))
$myRows[]=$row2;
$randomEntry=$myRows[array_rand($myRows)];
If you wanted a random field of that entry though, you should use array_rand like Bernhard Poiss pointed out.
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
$row2 = mysql_fetch_assoc($row);
$randomField=$row2[array_rand($row2)];
You will first want to populate an array of your results
$row = mysql_query("SELECT * FROM anunt WHERE lichidareStoc = 0 ORDER BY anuntID DESC") or die(mysql_error());
$rows = array();
while($rec = mysql_fetch_assoc($row)){
$rows[] = $rec;
}
Then you can select a random item using array_rand
$random = $rows[array_rand($rows)];

MySQL query within another query's while loop in PHP

I have the following code:
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($result = mysql_fetch_array($query)) {
extract($result);
if ($activity_type == "discussion") {
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($result = mysql_fetch_array($query)) {
extract($result);
echo $discussion_user . " said:<br>" . $discussion_text . "<br>";
}
} elseif ($activity_type == "file") {
}
}
But it just returns the last row. My goal is to have a chronological list of "activities" each displayed slightly differently depending on their type.
Your using $query and $result twice so the second loop is overwriting the result of the first and stopping...
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
and
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
same with $results var...
I would suggest you change to $query and $query2 but best to use something like
$activies = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($activity = mysql_fetch_array($activies)) {
and
$discussions = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($discussion = mysql_fetch_array($discussions)) {
I would also avoid using extract - as you might be overwriting vars your not expecting to...
You have to create another connection to the database so that you can run them at the same time.
OR
You can load the results of the first one into an array, and then just loop through the array.

How to display other values, when a query is limited by 3?

Can anyone tell me how to display the other values, when a query is limited my 3. In this question I asked how to order and limit values, but now I want to show the others in another query. How would I go about doing this?
Here's the code I used before:
$query = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
If display all rows use like this :
$query = "SELECT gmd FROM account ORDER BY gmd DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
If display all rows without that 3 rows use like this :
$query = "SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,1000000";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
}
if you LIMIT the resultset in the query you only get 3 rows returned.
if you want to show the rest, don't limit inside the query, but check the rowcount in your php loop
SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,9999999999
or may be you need pagination
SELECT gmd FROM account ORDER BY gmd DESC LIMIT 3,10
Will skip first 3 values and display next 10 ones satisfying the condition. This is MySQL only solution though.

Categories