Tag Archive for query

Preserve & update/rebuild query string

<?php
/**
* Name: Preserve and update/rebuild query string<br>
* @param Example:
* Example URL: <a href="http://www.site.com/?category=foo&order=desc&page=2" >http://www.site.com/?category=foo&order=desc&page=2</a>
*
* <a href="<?php echo queryString('order','asc'); ?>">Order ASC</a>
*
* Output HTML: <a href="?category=foo&amp;order=asc&amp;page=2">Order ASC</a>
* Output URL: <a href="http://www.site.com/?category=foo&order=asc&page=2" >http://www.site.com/?category=foo&order=asc&page=2</a>
*
* Not <a href="http://www.site.com/?category=foo&order=desc&page=2&order=asc" >http://www.site.com/?category=foo&order=desc&page=2&order=asc</a>
*/
function queryString($str,$val)
{
$queryString = array();
$queryString = $_GET;
$queryString[$str] = $val;
$queryString = "?".htmlspecialchars(http_build_query($queryString),ENT_QUOTES);

return $queryString;
}
?>

source

Find all categories NOT used in current item

function findunUsedCats($aid){
$query = "SELECT cat_tbl.cid, cat_tbl.category FROM cat_tbl WHERE cat_tbl.category NOT IN (SELECT cat_tbl.category FROM cat_tbl, art_has_cat_tbl WHERE cat_tbl.cid = art_has_cat_tbl.cid AND aid =$aid);";
$query = mysql_query($query)or die("nope..".mysql_error());
return $query;
}

source

Function to get first record from query and return single value

-- Person may have multiple addresses.  Gets FIRST record only
CREATE OR REPLACE FUNCTION get_first_address(person_id_in IN VARCHAR2) RETURN VARCHAR2 IS
ret_value VARCHAR2(1024) := ' ';

CURSOR the_cur(l_person VARCHAR2) IS
SELECT street1 || ', ' || street2
FROM address
WHERE person_id = l_person
ORDER BY date_added;
BEGIN
OPEN the_cur(l_person);

FETCH the_cur
INTO ret_value;

CLOSE the_cur;

RETURN ret_value;
EXCEPTION
WHEN no_data_found THEN
RETURN ret_value;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('EXCEPTION IN get_first_address - ' || SQLCODE || ': ' || SQLERRM);
RETURN ret_value;
END get_first_address;

source

from_unixtime sql

	public function listAllMyPublishedArticles($uid) {
$query="SELECT article_tbl.aid,header,introduction,body,published_state, FROM_UNIXTIME(published_date, '%d/%m/%y %H:%i') as published_date FROM article_tbl, user_has_art_tbl WHERE user_has_art_tbl.uid= $uid AND user_has_art_tbl.aid = article_tbl.aid AND article_tbl.published_state = 1 ORDER BY aid desc";
$query=mysql_query($query)or die(mysql_error());
return $query;
}

source

Print Query

print_r($queryname);die;

source

Show mysql query log with firebug

if (!function_exists('debug_mysql_query')) {

function debug_mysql_query($query)
{
$js_query = str_replace(array('', "'"), array("\", "'"), $query);
$js_query = preg_replace('#([x00-x1F])#e', '"x" . sprintf("%02x", ord("1"))', $js_query);
echo '<script>console.log("'.$js_query.'");</script>'."
";
return mysql_query($query);
}
}

source

Postgres output CSV

f ','
a

o /tmp/moocow.csv
SELECT foo,bar FROM whatever;
o
q

If a field has newlines, this will break. You can do something like this instead.....

SELECT foo, bar, '"' || REPLACE(REPLACE(field_with_newilne, '
', 'n'), '"', '""') || '"' FROM whatever;

source

Launch Google Search in default Browser

/// <summary>
/// Class to launch Google Search in default browser.
/// </summary>
public static class Google {
/// <summary>
/// Launches Google Search in default browser, and escapes string according to: <a href="http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#appendix_url_escaping" >http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#appendix_url_escaping</a>
/// </summary>
/// <param name="searchQuery">The search query.</param>
public static void SearchGoogle(string searchQuery) {
string fixedSearchQuery = null;

foreach (char character in searchQuery) {
if (Char.IsLetterOrDigit(character)) {
fixedSearchQuery += character;
} else if (character == Char.Parse(" ")) {
fixedSearchQuery += "+";
} else {
fixedSearchQuery += Uri.HexEscape(character);
}
}

string url = @"http://www.google.com/search?hl=en&q=" + fixedSearchQuery;

try {
Process.Start(url);
} catch { }
}
}

source

Template Query Debug

{% if debug %}
Queries

{{ sql_queries|length }} Quer{{ sql_queries|pluralize:"y,ies" }} {% ifnotequal sql_queries|length 0 %} (Show) {% endifnotequal %}
{% for query in sql_queries %}{% endfor %}
# 	SQL 	Time
{{ forloop.counter }} 	{{ query.sql|escape }} 	{{ query.time }}
{% endif %}

source

PHP connecting and query MySQL DB

<?php
//create db connection
$connection = mysql_connect("localhost", "user", "password");
if(!$connection)
{
die("Database connection failed:" . mysql_error());
}

// select a dtabase to use
$db_select = mysql_select_db("database name", $connection);
if(!$db_select)
{
die("Database selection failed:" . mysql_error());
}

?>

<body>
$result = mysql_query("SELECT * FROM tablename", $connection);

while ($row = mysql_fetch_array($result))
{
echo $row["rownamehere"]." ".$row["rownamehere"]."<br/>";
}
?>
</body>

<?php
mysql_close($connection);
?>

source