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
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
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
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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.