Jump to content

dirkers

Members
  • Posts

    13
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

dirkers's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. How about flipping the array while making the values arrays themselves, like so: <?php // original array $ar = array('Stuff' => 'Truck', 'Widgets' => 'Courier', 'Thingies' => 'Post', 'Doovas' => 'Truck'); // initializing new array $ar2 = array(); // flip key-value pairs // and stack values as needed foreach ($ar as $key => $val) { if (array_key_exists($val, $ar2)) { $ar2[$val][] = $key; } else { $ar2[$val] = array($key); } } // output foreach ($ar2 as $transportMethod => $product) { echo "Your " . implode(' and ', $product) . " will be delivered by $transportMethod\n"; } ?> This will output: Your Stuff and Doovas will be delivered by Truck Your Widgets will be delivered by Courier Your Thingies will be delivered by Post
  2. Looks like your script is using PEAR's DB Package. Since it is being included via require_once(), I don't think your script would have run without finding DB.php. In other words, your old server had the PEAR's DB Package installed and your new server will have to as well. The package was probably installed in a directory external to your project, which is why you didn't find it.
  3. Not sure what you mean. Can you give an example? -D
  4. Just a word of caution ... you can't necessarily assume that all those URL variations point to the same site. Although it is often the case, all of the following could be serving different sites: http://somesite.com https://somesite.com http://www.somesite.com https://www.somesite.com -D
  5. Slightly better to store the column names in an array rather than to pollute the name space. Since you don't necessarily know the names of the variables, you might be overwriting something unintentionally. Also, using an associative array to fetch the results makes it a little clearer, I think. $sql = "SHOW COLUMNS from answers"; $result = mysql_query($sql); // array to store column names $colNames = array(); // iterate over result set while ($row = mysql_fetch_assoc($result)) { // append column name to array $colNames[] = $row['Field']; } // output column names for testing print_r($colNames);
  6. empty() will have covered if the variable is an empty string or undefined. Don't use if (!$user) { }. However, be careful with empty() because it also returns TRUE for the integer 0 and string "0". For more details on empty(), take a look at the empty() PHP Manual page. Depending on your scenario, you might want to look at isset(). -D
  7. How about letting the DB do the work by using a sub-select: $q = "SELECT zip_code FROM companies WHERE ". $search." AND zip_code IN (SELECT zip FROM allowed_zip WHERE user_id='$id')"; Alternatively, you can rewrite the above query as an INNER JOIN or INTERSECT.
  8. The reason that you're not getting any error output might have something to do with the values you have for display_errors and error_reporting in your php.ini. BTW, to get more / better / nicer debug output, you might want to install Xdebug. As an added benefit, it protects against infinite recursion.
  9. In the phpinfo() output, inspect the values for safe_mode_exec_dir, safe_mode_gid, safe_mode_include_dir. Any of these might cause one script to execute just fine while another fails. Also, refer to the safe_mode documentation.
  10. Nearly as many lines as your function, but I think this is much easier to read: function detail_permissions($permission, $display, $proid, $userid, $title) { $info_to_display = '<tr><td id="tdtitlestyle">' . $title . '</td><td id="tdgap"></td><td>' . ucwords($display) . '</td></tr>'; $query = mysql_query("select * from friends where user_id = '$userq->id' and friend_id = '$id'"); $retVal = FALSE; if ($display) { switch ($permission) { case '1': $retVal = $info_to_display; break; case '2': $retVal = (mysql_num_rows($query) == 1 || $proid == $userid) ? $info_to_display : FALSE; break; case '3': $retVal = ($proid == $userid) ? $info_to_display : FALSE; break; } } return $retVal; } I hope it works, since I rewrote it without actually executing it. -D
  11. So you like them fancy queries? Well hold on to your hat cuz we're gonna make it a bit trickier still. The problem with the solution(s) thus far is that there is a race condition in the function. Any number of processes that are kicked off near simultaneously might retrieve the same listing of game servers and consequently come up with the same port number to assign. This is undesired behavior and needs to be remedied. Whether you handle it programmatically or in a query, figuring out which port to use next and to assign it to the newly created game server entry has to happen in an atomic transaction. Now, there are a number of ways you can do this, such as using transactions with the InnoDB storage engine. However, let's keep going with the fancy queries because we can. :-) Start by creating a trigger by executing the following SQL against the DB that contains the tables in question: CREATE TRIGGER find_port BEFORE INSERT ON GameServers FOR EACH ROW BEGIN SET NEW.Port = (SELECT (Port + 1) AS Port FROM GameServers WHERE (Port + 1) NOT IN (SELECT Port FROM GameServers) ORDER BY Port ASC LIMIT 1); END This will cause the port to be assigned automatically each time you insert a new entry into the GameServer table. Nice, but it doesn't yet fix our race condition. To do that we need to lock the table for writing while we add the new entry and assign the port. We can do that by issuing a LOCK TABLE statement, which we later undo with UNLOCK TABLES. <?php function CreateNewGameServer($game, $slots, $days, $owner, $server) { $CleanGame = mysql_real_escape_string($game); $CleanSlots = mysql_real_escape_string($slots); $CleanDays = mysql_real_escape_string($days); $CleanOwner = mysql_real_escape_string($owner); $CleanServer = mysql_real_escape_string($server); $FindServer = mysql_query("SELECT * FROM Servers WHERE ServerName = '".$CleanServer."'"); if (mysql_num_rows($FindServer) == 1) { // Check that the clients on the box are under 20 if (GetClientsOnServer($CleanServer) >= 20) { echo"<p>Error, The specified server is currently full.</p>"; } else { // lock tables to fix race condition mysql_query("LOCK TABLE GameServer WRITE"); // create server entry $result = mysql_query("INSERT INTO GameServers (Owner, Game, Slots, Expire, OnServer, DatePurchased) VALUES('".$CleanOwner."', '".$CleanGame."', '".$CleanSlots."', DATE_ADD(NOW, INTERVAL '.$CleanDays.' DAYS), '".$CleanServer."', NOW())"); // release table lock mysql_query("UNLOCK TABLES"); // get the new ID that was generated. $ServerID = mysql_insert_id(); // find lowest port such that port+1 has no entry in `GameServers` // and update new server $Result = mysql_query("UPDATE `chrismar_xhost`.`GameServers` SET `Executable` = 'server".$ServerID."', `Directory` = '/home/server".$ServerID."', WHERE `GameServers`.`ServerID` = '".$ServerID."' LIMIT 1 "); } } else { echo"<p>Invalid server specified.</p>"; } } ?> For the above code, one should use the mysql_connect() function instead of mysql_pconnect() so as to avoid locked tables if the code gets interrupted before it can execute the UNLOCK TABLES statement. As for performance, yes, it would be better for forgo the queries, but what's the fun in that? But seriously, this is not a high-volume function. If this function ever affects performance, it's because Chris is selling hundred of thousands of servers a day. By then, he can afford to throw a little hardware at the problem. -D
  12. ok, I tweaked the query to my earlier posting so it finds the lowest available port number. As for "method of comparing time" cb refers to, I don't think that my code omitted any functionality (read the SQL carefully). <?php function CreateNewGameServer($game, $slots, $days, $owner, $server) { $CleanGame = mysql_real_escape_string($game); $CleanSlots = mysql_real_escape_string($slots); $CleanDays = mysql_real_escape_string($days); $CleanOwner = mysql_real_escape_string($owner); $CleanServer = mysql_real_escape_string($server); $FindServer = mysql_query("SELECT * FROM Servers WHERE ServerName = '".$CleanServer."'"); if (mysql_num_rows($FindServer) == 1) { // Check that the clients on the box are under 20 if (GetClientsOnServer($CleanServer) >= 20) { echo"<p>Error, The specified server is currently full.</p>"; } else { // create server entry $result = mysql_query("INSERT INTO GameServers (Owner, Game, Slots, Expire, OnServer, DatePurchased) VALUES('".$CleanOwner."', '".$CleanGame."', '".$CleanSlots."', DATE_ADD(NOW, INTERVAL '.$CleanDays.' DAYS), '".$CleanServer."', NOW())"); // get the new ID that was generated. $ServerID = mysql_insert_id(); // find lowest port such that port+1 has no entry in `GameServers` $FindPort = mysql_query("SELECT (Port + 1) AS NewPort FROM `GameServers` WHERE (Port + 1) NOT IN (SELECT Port FROM `GameServers`) ORDER BY NewPort ASC LIMIT 1"); $Port = mysql_result($FindPort, 0); // update new server $Result = mysql_query("UPDATE `chrismar_xhost`.`GameServers` SET `Executable` = 'server".$ServerID."', `Directory` = '/home/server".$ServerID."', `Port` = '".$Port."' WHERE `GameServers`.`ServerID` = '".$ServerID."' LIMIT 1 "); echo($Port); } } else { echo"<p>Invalid server specified.</p>"; } } ?>
  13. The idea was to simplify. I would suggest to let MySQL do some of the heavy lifting. Using native MySQL functions NOW(), MAX(), IF(), and ISNULL(), it can take care of the timestamps and finding a new port number. It's hard to rewrite this without being able to run it, but take a look at this: <?php function CreateNewGameServer($game, $slots, $days, $owner, $server) { $CleanGame = mysql_real_escape_string($game); $CleanSlots = mysql_real_escape_string($slots); $CleanDays = mysql_real_escape_string($days); $CleanOwner = mysql_real_escape_string($owner); $CleanServer = mysql_real_escape_string($server); $FindServer = mysql_query("SELECT * FROM Servers WHERE ServerName = '".$CleanServer."'"); if (mysql_num_rows($FindServer) == 1) { // Check that the clients on the box are under 20 if (GetClientsOnServer($CleanServer) >= 20) { echo"<p>Error, The specified server is currently full.</p>"; } else { // create server entry $result = mysql_query("INSERT INTO GameServers (Owner, Game, Slots, Expire, OnServer, DatePurchased) VALUES('".$CleanOwner."', '".$CleanGame."', '".$CleanSlots."', DATE_ADD(NOW, INTERVAL '.$CleanDays.' DAYS), '".$CleanServer."', NOW())"); // get the new ID that was generated. $ServerID = mysql_insert_id(); // find highest existing port + 1; default port 30000 if none yet exists $FindPort = mysql_query("SELECT IF(ISNULL(MAX(Port)), 30000, MAX(Port) + 1) newPort FROM `GameServers`"); $Port = mysql_result($FindPort, 0); // update new server $Result = mysql_query("UPDATE `chrismar_xhost`.`GameServers` SET `Executable` = 'server".$ServerID."', `Directory` = '/home/server".$ServerID."', `Port` = '".$Port."' WHERE `GameServers`.`ServerID` = '".$ServerID."' LIMIT 1 "); echo($Port); } } else { echo"<p>Invalid server specified.</p>"; } } ?> I wasn't totally clear on how you determine a port number. Can different servers have the same port number or is it globabally unique? The way I coded it above, it finds a globally unique port number and starts with a default of 30000 if none if given in the GameServers table.
×
×
  • 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.