Location: PHPKode > projects > SQLite PHP Admin > spa/query.php
<?php
/***********************************************************************

		Copyright (c) 2011, Andrew V. Pleshakov	(hide@address.com)
		All rights reserved.

		This file is part of SPA (SQLite PHP Admin)
		See file LICENSE.txt for details

************************************************************************/

require_once('include.php');
if(!$pla_db) {
	header('Location: main.php');
	die;
}

$cur_tab = '';
if(isset($_REQUEST['object'])) $cur_tab = $_REQUEST['object'];
$tar['table'] = $cur_tab;

if (isset($_POST['sql'])) {
	$tar['show'] = htmlentities($_POST['sql'],ENT_QUOTES,DEF_CHARSET);
	$_POST['sql'] = iconv(DEF_CHARSET,$encoding,trim($_POST['sql']));
}
else if($cur_tab)
	$tar['show'] = "SELECT * FROM '$cur_tab' LIMIT 50";
else $tar['show'] = '';

$sql = '';
$tar['exec'] = false;
if (trim($_POST['sql'])) {
	$sql ="<br />\n<table>\n";
	$tar['exec'] = true;
	$rows_affected = 0;
	$time_start = microtime(true);
	if(isset($_POST['exec'])) { // run resultless query
		if($pla_db->exec(trim($_POST['sql']))) {
			$time_tot = round(microtime(true) - $time_start,5);
			$rows_affected = $pla_db->affectedRows();
			$rows_returned = 0;
			$sql .= design_render('misc/sqlr',array('affected'=>$rows_affected,'returned'=>$rows_returned,'total'=>$time_tot));
			$sql .= '<tr><td>&nbsp;</td></tr>';
		}
		else
			$sql .= design_render('misc/sqle',array('err'=>$pla_db->error));
	}
	else if($pla_db->query(trim($_POST['sql']))) {
		$time_tot = round(microtime(true) - $time_start,5);
		$rows_affected = $pla_db->affectedRows();
		$rows_returned = 0;
		$buf = '';
		while($row = $pla_db->fetchArray()) {
			$rows_returned++;
			$nr_fields = count($row);
			$buf .= "<tr>\n";
			for ($i=0; $i<$nr_fields; $i++)
					$buf .= '<td>'.htmlentities(iconv($encoding,DEF_CHARSET,$row[$i]),ENT_QUOTES,DEF_CHARSET)."</td>\n";
			$buf .= "</tr>\n";
			if($rows_returned >= MAX_QUERY_LENGTH) {
				$buf .= design_render('misc/sqlint',array('f'=>$nr_fields,'len'=>MAX_QUERY_LENGTH));
				break;
			}
		}
		$sql .= design_render('misc/sqlr',array('affected'=>$rows_affected,'returned'=>$rows_returned,'total'=>$time_tot));
		if ($rows_returned) {
			$sql .= '<tr>';
			$nf = $pla_db->numFields();
			for ($i=0; $i<$nf; $i++) // column names
				$sql .= "<th>".$pla_db->fieldName($i)."</th>\n";
			$sql .= '</tr>';
			$sql .= $buf;
		}
		else $sql .= '<tr><td>&nbsp;</td></tr>';
	}
	else
		$sql .= design_render('misc/sqle',array('err'=>iconv($encoding,DEF_CHARSET,$pla_db->error)));
	$sql .= "</tbody>\n</table>\n";
}
$tar['sql'] = $sql;
die(design_render('query',$tar));
Return current item: SQLite PHP Admin