Jump to content

Random field update


Shadowing

Recommended Posts

Im wanting to update a random field in a table and only update the fields that are empty

 

is this the best way to do this. im not sure if WHERE id = ' ' will only choose a blank field

 

 <?php mysql_query("UPDATE systems SET id= '".($_SESSION['user_id'])."' , owner = '".($_SESSION['user_id'])."' WHERE id = '' ORDER BY RAND() LIMIT 1"); ?> 

Link to comment
https://forums.phpfreaks.com/topic/254118-random-field-update/
Share on other sites

this is what i came up with from googling another way to do this cause i would like to keep performance in mind. anychance you can help me out with generating a random id

 <?php
	                             // search how many rows there are
                                 $sql = "SELECT * AS  id FROM systems"; 					
			$query = mysql_query($sql) or die(mysql_error());					
			$fetch = mysql_fetch_row($query);
                                                                                
                                                                // add the number of rows to a variable
                                  $rows = $fetch['id'];

//I dont know how to generate a random number to use in the next query


			$sql = "SELECT  id FROM systems LIMIT $random_number, 1 "; 			
			$query = mysql_query($sql) or die(mysql_error());					
			$fetch = mysql_fetch_array($query);
                                                          
                                   $id = $fetch['id'];
?> 

Link to comment
https://forums.phpfreaks.com/topic/254118-random-field-update/#findComment-1302947
Share on other sites

on second thought, after looking at the OP again, the original sql statement that you have will work, if you are only worried about updating one random field.

 

<?php mysql_query("UPDATE systems SET id= '".($_SESSION['user_id'])."' , owner = '".($_SESSION['user_id'])."' WHERE id = '' ORDER BY RAND() LIMIT 1"); ?> 

Link to comment
https://forums.phpfreaks.com/topic/254118-random-field-update/#findComment-1302953
Share on other sites

on account setup a player gets assigned a planet in the table

 

so I randomly find a planet that isnt assigned to anyone and assign it to them by adding their player id to it.

 

Im making a physical map so the planet cant just be created. Has to be chosen

Link to comment
https://forums.phpfreaks.com/topic/254118-random-field-update/#findComment-1302983
Share on other sites

  Quote

what a relief ha

 

plus this script is only ran when people start or reset their accounts. so that has to help alot right?

well, yeah I suppose, I'm not a fan of order by rand(), however technically in this case it will work and shouldn't affect efficiency.

Link to comment
https://forums.phpfreaks.com/topic/254118-random-field-update/#findComment-1302995
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.