Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

shantanu oak http://oksoft.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 26 total  RSS 

running total in PHP

// output running total
// userID,posts,runningTotal|
// 2,23434,28330|
// 6,3443,4896|
// 1,422,1453|
// 3,344,1031|
// 4,344,687|
// 5,343,343|

   1  
   2  $q = mysql_query("select * from `members` order by `posts` DESC");
   3  echo 'userID,posts,runningTotal|<br>';
   4  while($a = mysql_fetch_row($q)){
   5  echo "$a[0],$a[1],$total|<br>";
   6  $total = $total - $a[1];
   7  }  

Compare Engines

// If you have 2 servers with identical database structure, and some of the tables have different engine type, then create a federated table to connect to the original server and compare the engines type with the current table's engine.

   1  
   2  CREATE TABLE test.`TABLES2` (
   3  `TABLE_CATALOG` varchar(512) default NULL,
   4  `TABLE_SCHEMA` varchar(64) NOT NULL default '',
   5  `TABLE_NAME` varchar(64) NOT NULL default '',
   6  `TABLE_TYPE` varchar(64) NOT NULL default '',
   7  `ENGINE` varchar(64) default NULL,
   8  `VERSION` bigint(21) default NULL,
   9  `ROW_FORMAT` varchar(10) default NULL,
  10  `TABLE_ROWS` bigint(21) default NULL,
  11  `AVG_ROW_LENGTH` bigint(21) default NULL,
  12  `DATA_LENGTH` bigint(21) default NULL,
  13  `MAX_DATA_LENGTH` bigint(21) default NULL,
  14  `INDEX_LENGTH` bigint(21) default NULL,
  15  `DATA_FREE` bigint(21) default NULL,
  16  `AUTO_INCREMENT` bigint(21) default NULL,
  17  `CREATE_TIME` datetime default NULL,
  18  `UPDATE_TIME` datetime default NULL,
  19  `CHECK_TIME` datetime default NULL,
  20  `TABLE_COLLATION` varchar(64) default NULL,
  21  `CHECKSUM` bigint(21) default NULL,
  22  `CREATE_OPTIONS` varchar(255) default NULL,
  23  `TABLE_COMMENT` varchar(80) NOT NULL default ''
  24  )
  25  ENGINE=FEDERATED DEFAULT CHARSET=latin1
  26  CONNECTION='mysql://root@172.172.172.172/information_schema/TABLES';
  27  
  28  SELECT b.TABLE_SCHEMA as remote_database, b.TABLE_NAME as remote_tableName, b.ENGINE as remote_engine, a.ENGINE AS local_engine 
  29  FROM test.TABLES2 AS a INNER JOIN information_schema.TABLES as b 
  30  ON a.TABLE_SCHEMA = b.TABLE_SCHEMA AND a.TABLE_NAME = b.TABLE_NAME AND a.ENGINE != b.ENGINE;

server monitoring

# servers.sh
export MAILSERVERS="server1 server2 server3"
export WEBSERVERS="www1 www2 www3 www4"

#!/bin/bash
### Assess disk space on mail servers
source ./servers.sh
for i in ${MAILSERVERS} ; do
echo =========${i} =============
ssh root@${i} "df"
echo ============ =============
done

# min hour dom month dow command

grep “###” *

Querystring variables

   1  
   2  // Add Querystring Variable
   3  // A PHP function that will add the querystring variable $key with a 
   4  // value $value to $url. If $key is already specified within $url, 
   5  // it will replace it.
   6  
   7  function add_querystring_var($url, $key, $value) {
   8  $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
   9  $url = substr($url, 0, -1);
  10  if (strpos($url, '?') === false) {
  11  return ($url . '?' . $key . '=' . $value);
  12  } else {
  13  return ($url . '&' . $key . '=' . $value);
  14  }
  15  }
  16  
  17  // Remove Querystring Variable
  18  // A PHP function that will remove the variable $key and its value 
  19  // from the given $url.
  20  
  21  function remove_querystring_var($url, $key) {
  22  $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
  23  $url = substr($url, 0, -1);
  24  return ($url);
  25  }
  26   

delete empty directories

   1  
   2  // commands to recursively delete empty directories below the current one
   3  // (use at your own risk, as the slightest mistake WILL destroy 
   4  // all of your data - Linux) 
   5  find -depth -type d -empty -exec rmdir {} \;

Unicode words from online dictionary

// list words from unicode dictionary
// you need to add this line in the head section
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

   1  
   2  for ( $i = 1; $i <= 45; $i++) {
   3  $url="http://dsal.uchicago.edu/cgi-bin/romadict.pl?page=$i&table=molesworth&display=utf8";
   4  $text=file_get_contents($url);
   5  $myarray = preg_match_all('#<font size="\+1">(.*?)</font>#i', $text, $matches);
   6  echo implode(' ',$matches[1]); 
   7  }

extract table names from sql log file

   1  
   2  grep "from " /var/log/mysql/mysqld.log | awk -Ffrom '{print $2}' | awk '{print $1}' | cat > /home/shantanu/testing.txt

Password Protection

// Bare Bones demo of password protection

   1  
   2  <?php
   3  $password = "abc123";
   4  if ($_POST[doddle] == $password) {
   5          $tell = 1;
   6  } else {
   7          $tell = 2;
   8  }
   9  if (! is_string($_POST[doddle])) $tell = 0;
  10  ?>
  11  <html>
  12  <head><title>A Page that is password protected</title></head>
  13  <body>
  14  <?php if ($tell == 1) { ?>
  15   
  16  <h1>The page with all the secrets revealed </h1>
  17   
  18  This is the information that has been revealed to you because you
  19  got the password right!<br /><br />Although this is a very simple
  20  demo it shows what you can do with just a few lines of PHP.
  21   
  22  <?php } else { ?>
  23   
  24  <h1>A Header page</h1>
  25   
  26  There is a page hidden under a password at this URL. For this demo only
  27  I will tell you that the password is "abc123" so you can try it out!<br />
  28  <?php if ($tell == 2) print ("<br />YOU GOT THE PASSWORD WRONG<br />"); ?>
  29  <br />
  30  <form method=post>
  31  Please enter password <input type=password name=doddle>
  32  <input type=submit value=go>
  33  </form>
  34   
  35  <?php } ?>
  36   
  37  <hr />
  38  <a href=http://www.wellho.net>Well House Consultants</a>, 2007
  39  </body>
  40  </html>

tinyurl explode

// check where tinyurl.com is headed to

   1  
   2      < ?php
   3  
   4      // tinyurl.php?c=
   5  
   6      $num = $_GET['c'];
   7  
   8      if($fp = fsockopen ("tinyurl.com", 80, $errno, $errstr, 30))
   9      {
  10      if ($fp) {
  11      fputs ($fp, "HEAD /$num HTTP/1.0\r\nHost: tinyurl.com\r\n\r\n");
  12      while (!feof($fp)) {$headers .= fgets ($fp,128);}
  13      fclose ($fp);
  14      }
  15      $arr1=explode("Location:",$headers);
  16      $arr=explode("\n",trim($arr1[1]));
  17      echo trim($arr[0]);
  18      }
  19      ?>

mysql dump to another server

// Using UNIX pipe concept one can dump database to another server securely using ssh protocol.
// All you need remote execution rights for the ‘dd’ command, over SSH.

   1  
   2  mysqldump -u USERnAME -p'PASSWORD' YOUR-DATABASE-NAME | ssh user@remote.server.com "dd of=/mysql/$(date +'%d-%m-%y')"
« Newer Snippets
Older Snippets »
Showing 1-10 of 26 total  RSS