Tag Archive for query

Format a date as desired in a LINQ query assigned object

Created = String.Format("{0:M/d/yyyy}", cn.Created)

source

Simple Query Menager – XClass

<?php
class x {
	public function query($query){
		return new Query($query);
	}
}

class Query {
	public function Query($query, $sql){
		$this->query = $query;
		$this->sql = $sql;
	}
	
	public function attr(){
		$get = func_get_args();
		$num = func_num_args()+1;
				
		for($i=1; $i<$num; ++$i) $this->query = str_replace("%".$i, $get[$i-1], $this->query);
		return $this;
	}
	
	public function cut($num){
		if(strlen($this->query) > $num){
			$this->query = substr($this->query, 0, $num)."...";
		}
		
		return $this;
	}
	
	public function finish(){ return $this->query; }
	
	public function mail($pattern='/^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)+$/'){ return preg_match($pattern, $this->query)?true:false; }
	
	public function hash(){
		$this->query = sha1($this->query);
		return $this;
	}
	
	public function md5(){
		$this->query = md5($this->query);
		return $this;
	}
	
	public function protect(){
		if(get_magic_quotes_runtime()){
			$this->query = htmlspecialchars(stripslashes($this->query));
		}
		
		$this->query = @mysql_real_escape_string($this->query);
		return $this;
	}
	
	public function bb($type=false){
		$str = $this->query;
		$str = htmlspecialchars($str);
		$str = nl2br($str);
		
		$str = preg_replace("#<b>(.*?)</b>#si", '<b>1</b>', $str);
		$str = preg_replace("#<i>(.*?)</i>#si", '<i>1</i>', $str);
		$str = preg_replace("#<u>(.*?)</u>#si", '<u>1</u>', $str);
		$str = preg_replace("#[s](.*?)[/s]#si", '<span class="s">1</span>', $str);
		
		$str = preg_replace("#<img src="(.*?)">#si", '<img src="1" alt="" />', $str);
		$str = preg_replace("#[img=(.*?)](.*?)[/img]#si", '<img src="1" alt="2" />', $str);
		
		$str = preg_replace("#<a href="(http.*?)">(http.*?)</a>#si", "<a href="1">1</a>", $str);
		$str = preg_replace("#<a href="(http.*?)">(.*?)</a>#si", "<a href="1">2</a>", $str);
		$str = preg_replace("#<a href="(.*?)">(.*?)</a>#si", "<a href="<a href="http://1">1</a>"," >http://1">1</a>",</a> $str);
		$str = preg_replace("#<a href="(.*?)">(.*?)</a>#si", "<a href="<a href="http://1">2</a>"," >http://1">2</a>",</a> $str);
		
		$str = preg_replace("#[quote](.*?)[/quote]#si",'<blockquote class="cytat">1</blockquote>',$str);
		$str = preg_replace("#[quote=(.*?)](.*?)[/quote]#si",'<p><cite>1</cite> napisa�(a):</p><blockquote>2</blockquote>',$str);
		$str = preg_replace("#
(.*?)

#si",'<pre>1</pre>',$str);
$str = preg_replace("#[hr=([0-9]{1,2}|100)]#si", '<hr class="linia" width="1%">', $str);

$this->query = $str;

return $type?$this:$str;
}
}
?>


source

Retrieve Query String Variables Javascript

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[[]/,"[").replace(/[]]/,"]");
  var regex = new RegExp("[?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

source

Fetch $_GET array as a string representation

<?php
/**
 * Fetches back a string representation of the current $_GET array.
 *
 * @version 1.0
 * @author Andrew Hart
 *
 * @param array $exclusions[optional] Which items of the $_GET array to skip.
 * @return string The $_GET array in string format, eg: "action=add&value=test&this=true"
 */
function fetch_query_string($exclusions = array())
{
	$str = "";
	
	foreach ($_GET as $key => $val)
	{
		if (!in_array($key, $exclusions))
		{
			$str .= $key . "=" . $val . "&";
		}
	}
	
	$str = substr($str, 0, strlen($str) - 1);
	
	return $str;
}
?>

source

getResultFromSQL(String) As Variant

Private Function getResultFromSQL(sql As String) As Variant
  Dim recordset As recordset
  
  Set recordset = CurrentDb.OpenRecordset(sql)
  
  If (Not recordset.EOF) _
   And (Not recordset.BOF) Then
    recordset.MoveFirst
    getResultFromSQL = recordset(0)
    
    Set recordset = Nothing
    Exit Function
  End If
  
  Set recordset = Nothing
  getResultFromSQL = Nothing
End Function

source

Display database query (MySQL / Drupal 5.x)

<?php
function show_query($sql){
  $rows = array();
  $header = array();
  $result = db_query($sql);
  while($row = db_fetch_array($result)){
    $rows[] = $row;
  }
  $i = 0;
  while ($i < mysql_num_fields($result)) {
    $meta = mysql_fetch_field($result, $i);
    $header[] = $meta->name;
    $i++;
  }
  print theme_table($header, $rows);
}
show_query("SELECT name, mail FROM {users} LIMIT 10");
?>

source

Find all databases and login credentials

SELECT domains.name AS domain_name,
data_bases.name AS database_name, db_users.login, accounts.password
FROM data_bases, db_users, domains, accounts
WHERE data_bases.dom_id = domains.id
AND db_users.db_id = data_bases.id
AND db_users.account_id = accounts.id
ORDER BY domain_name;

source

RectangleTest.java

public class RectangleTest {

public static void main(String[] args) {
Rectangle small = new Rectangle(2.0,3.0);
Rectangle noArgs = new Rectangle();
Rectangle large = new Rectangle(20.4,30.6);
Rectangle notDouble = new Rectangle(4,6);
Rectangle stupid = new Rectangle(3.2,400.1);

System.out.println("Small rectangle: A=" + small.getArea() + " P="+small.getPerimeter());
System.out.println("Default rectangle: A=" + noArgs.getArea() + " P="+noArgs.getPerimeter());
System.out.println("Large rectangle: A=" + large.getArea() + " P="+large.getPerimeter());
System.out.println("Rectangle not written as double: A=" + notDouble.getArea() + " P="+notDouble.getPerimeter());
System.out.println("Stupidly proportioned rectangle: A=" + stupid.getArea() + " P="+stupid.getPerimeter());
}

}

source

Rectangle.java

public class Rectangle {
private double length;
private double width;
private double area;
private double perimeter;

public Rectangle(){
this.length = 1.0;
this.width = 1.0;
}

public Rectangle(double theLength, double theWidth){
length = theLength;
width = theWidth;
}

public double getArea() {
area = width * length;
return area;
}

public double getPerimeter(){
perimeter = (width * 2) + (length * 2);
return perimeter;
}
}

source

JavaScript Query Selector

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<p id="hello">

</p>

<p class="goodbye">

</p>

<script>
// Gather the elements
var i = document.querySelector("p#hello");
i.innerHTML = "Hello World";

var j = document.querySelector("p.goodbye");
j.innerHTML = "Good Bye!";
</script>

</body>
</html>

source