Randomizing Data From A Table
Posted by: iamruthermelchor (130.105.45.---)
Date: November 13, 2017 04:30AM

I'm trying to randomize a data from my SQL database using PHP - and I'm displaying the results in an HTML page.

It works fine using this code:

$random = mysqli_query($con,"SELECT * FROM tbData WHERE Selected != 0 order by RAND() LIMIT 1" ) ;
$row = mysqli_fetch_array($random);

I'm retrieving the names in the 'Name' column.

The problem is I want to prevent duplicates from being 'drawn'; so I added a second column named 'Selected' and added '1' as default to all records. So the code snippet above selects from any records that has '1' in the 'Selected' column, so logically, I need to turn this '1' into a '0' to prevent it from being 'drawn'.

I'm working with this second code block to do it, but it seems to fail in changing the '1's to '0's:

$random1 = mysqli_query($con,"UPDATE tbData SET Selected = '0' WHERE Name = ".$row['Name']." " ) ;

Any help with this?

Edited 1 time(s). Last edit at 11/13/2017 05:30AM by iamruthermelchor.

Options: ReplyQuote
Re: Randomizing Data From A Table
Posted by: Otomatic (Moderator)
Date: November 13, 2017 09:24AM


This question is not On Topic
This site is for people having issues getting WAMPServer running and not a general HOW DO I site for everything web or database or PHP related.

But, as we are very nice, look at DISTINCT in the syntax of a SELECT query:
I will not give any answer on this subject. Check out sites related to MySQL and its queries.

Wampserver 3.1.3 32 bit - Apache 2.4.33 - PHP 7.2.7/7.1.19/7.0.30/5.6.36 - MySQL 5.7.22 - MariaDB 10.3.7
Wampserver 3.1.3 64 bit - Apache 2.4.33 - PHP 7.2.7/7.1.19/7.0.30/5.6.36 - MySQL 5.7.22 - MariaDB 10.3.7
PhPMyAdmin 4.8.1 - MysqlDumper 1.24.5
on W10 and W7 Pro 64 bit
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons
« Ce n'est pas parce qu'ils sont nombreux à avoir tort, qu'ils ont forcément raison. Coluche »
« It's not because they are many to be wrong, they are necessarily right. Coluche »

Options: ReplyQuote

Sorry, only registered users may post in this forum.