Skip to content

Instantly share code, notes, and snippets.

@morrislaptop
Created August 23, 2009 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morrislaptop/173071 to your computer and use it in GitHub Desktop.
Save morrislaptop/173071 to your computer and use it in GitHub Desktop.
function db_query($query)
{
$result = mysql_query($query) or sql_error($query);
return $result;
}
function sql_error($query)
{
$message = "DATABASE ERROR \n QUERY: $query \n ERROR: " . mysql_error();
$out = '<hr><strong>DATABASE ERROR</strong><hr><pre><p><div><em>The query was</em> : </div><div>' . htmlentities($query) . '</div></p><p><div><em>MySQL Said</em> : </div><div>' . mysql_error() . '</div></p></pre><hr>';
echo $out;
}
function result2array($result, $pk='')
{
$array = array();
while ($row = mysql_fetch_assoc($result))
{
$count = count($row);
if ($count==1)
{
$array[] = current($row);
}
else if ($count==2)
{
$array[current($row)] = next($row);
}
else
{
if (!$pk) $array[] = $row;
else $array[$row[$pk]] = $row;
}
}
return $array;
}
function dataToSql($data)
{
$pairs = array();
foreach ($data as $field => $val)
{
if ( '(' == substr($val, 0, 1) && ')' == substr($val, -1) ) {
// raw entry, eg (NOW())
$pairs[] = $field . ' = ' . $val;
}
else if ( is_numeric($val) ) {
// numeric eg, 1, 4.34
$pairs[] = $field . ' = ' . $val;
}
else if ( is_bool($val) ) {
// boolean eg true, false
$pairs[] = $field . ' = ' . ($val ? 1 : 0);
}
else if ( is_array($val) ) {
// array
$pairs[] = $field . ' = \'' . mysql_escape_string(implode(',', $val)) . '\'';
}
else {
// string
$pairs[] = $field . ' = \'' . mysql_escape_string($val) . '\'';
}
}
return implode(', ', $pairs);
}
/**
* Prints debugging information about variables.
*
* @param mixed.. $var Variables to debug
* @return mixed First variable passed
*/
function trace()
{
$vars = func_get_args();
foreach ($vars as $var)
{
echo "<pre style='background-color: #F0F6FC; border: 1px solid #ccc; padding: 5px; overflow: auto; margin: 5px; max-height: 250px; color: black; font-size: 12px; font-family: Arial; text-align: left;'>";
if (is_string($var))
{
var_dump(htmlspecialchars($var));
}
else
{
var_dump($var);
}
echo '</pre>';
}
return $vars[0];
}
/**
* Prints debugging information about variables.
*
* trace2('before value', $old_value, 'after value', $new_value, ....)
*
* @param mixed.. $var Variables to debug
* @return mixed First variable passed
*/
function trace2()
{
$vars = func_get_args();
$c = func_num_args() - 1;
for ($i = 0; $i < $c; $i += 2)
{
echo "<pre style='background-color: #F0F6FC; border: 1px solid #ccc; padding: 5px; overflow: auto; margin: 5px; max-height: 250px; color: black; font-size: 12px; font-family: Arial; text-align: left;'><strong>{$vars[$i]}</strong><br />";
if (is_string($vars[$i+1]))
{
var_dump(htmlspecialchars($vars[$i+1]));
}
else
{
var_dump($vars[$i+1]);
}
echo '</pre>';
}
return $vars[1];
}
function export($data, $rowSep, $colSep, $opts = array())
{
$defaults = array(
'rowReplace' => ' ',
'rowEscape' => false,
'colReplace' => false,
'colEscape' => "\\"
);
$opts = array_merge($defaults, $opts);
$lines = array();
foreach ($data as $row)
{
foreach ($row as $i => $col)
{
$search = array();
$replace = array();
if ( $opts['rowReplace'] !== false ) {
$search[] = $rowSep;
$replace[] = $opts['rowReplace'];
}
if ( $opts['rowEscape'] !== false ) {
$search[] = $rowSep;
$replace[] = $opts['rowEscape'] . $rowSep;
}
if ( $opts['colReplace'] !== false ) {
$search[] = $colSep;
$replace[] = $opts['colReplace'];
}
if ( $opts['colEscape'] !== false ) {
$search[] = $colSep;
$replace[] = $opts['colEscape'] . $colSep;
}
$row[$i] = str_replace($search, $replace, $col);
}
$lines[] = implode($colSep, $row);
}
return implode($rowSep, $lines);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment