Jump to content

[solved] A Few Mysql Questions


Distortion

Recommended Posts

I have a database with about a hundred planets, each with 5 sectors, and on registration I want to assign a user with one of these sectors.I figured I needed an update query that found the first empty sector and filled in the username there. What I've come up with is this:

include("includes/connect.php");$query = "UPDATE sectors SET owner = '$username'WHERE owner = LIMIT 0 , 1";$result = mysql_query($query);

This doesn't do anything at all, and I also need to order the query by 'planet' and 'sector'.Can you help me out please?

Link to comment
Share on other sites

So if I understand this correctly, you have a mysql table with planets that has a column called: "Owner". You want to make a script that automaticly assigns owners from the usertable into the planet table.I think you will have to load both tables completely:

<?phpinclude("includes/connect.php");//Get planets where OwnerID is empty and put them into a array $PlanetList = Array();$sql = $db->sql_query("SELECT * FROM planets WHERE OwnerID = NULL");while($planets = $db->sql_fetchrow($sql)) {  array_push($PlanetList, $planets );}//Get all Users and loop trough them$sql = $db->sql_query("SELECT * FROM users");while($users= $db->sql_fetchrow($sql)) {  //loop trough every user and check if there ID numbers already exists in the planet OwnerID list.  if(!in_array($users["UserID"], $planets))  {	 //User doesn't exist on the planetList yet, so put your update query here  }  else  {	echo "This users has already been assigned to one of the planets";  }}?>

Link to comment
Share on other sites

oh I've been unclear... I'm sorry.I want to assign a user to an empty sector on registration. But my query didn't work and I found out why, the LIMIT thing was wrong. (This took around 12 deleted databases to find out, it kept filling my whole list with the same user :))The query:

"UPDATE sectors SET owner = '$username'		WHERE owner ='' LIMIT 1";

works and even picks the first available planet automatically, because my database is set up so that I don't have to order by the sectors and planets.Thanks for the effort anyways!

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...