示例#1
0
function _db_query($sql, $cluster, $shard)
{
    $cluster_key = _db_cluster_key($cluster, $shard);
    if (!$GLOBALS['db_conns'][$cluster_key]) {
        _db_connect($cluster, $shard);
    }
    $trace = _db_callstack();
    $use_sql = _db_comment_query($sql, $trace);
    $start = microtime_ms();
    $result = @mysql_query($use_sql, $GLOBALS['db_conns'][$cluster_key]);
    $end = microtime_ms();
    $GLOBALS['timings']['db_queries_count']++;
    $GLOBALS['timings']['db_queries_time'] += $end - $start;
    log_notice('db', "DB-{$cluster_key}: {$sql} ({$trace})", $end - $start);
    #
    # profiling?
    #
    $profile = null;
    if ($GLOBALS['cfg']['db_profiling']) {
        $profile = array();
        $p_result = @mysql_query("SHOW PROFILE ALL", $GLOBALS['db_conns'][$cluster_key]);
        while ($p_row = mysql_fetch_array($p_result, MYSQL_ASSOC)) {
            $profile[] = $p_row;
        }
    }
    #
    # build result
    #
    if (!$result) {
        $error_msg = mysql_error($GLOBALS['db_conns'][$cluster_key]);
        $error_code = mysql_errno($GLOBALS['db_conns'][$cluster_key]);
        log_error("DB-{$cluster_key}: {$error_code} " . HtmlSpecialChars($error_msg));
        $ret = array('ok' => 0, 'error' => $error_msg, 'error_code' => $error_code, 'sql' => $sql, 'cluster' => $cluster, 'shard' => $shard);
    } else {
        $ret = array('ok' => 1, 'result' => $result, 'sql' => $sql, 'cluster' => $cluster, 'shard' => $shard);
    }
    if ($profile) {
        $ret['profile'] = $profile;
    }
    return $ret;
}
示例#2
0
            $_POST['post_content'] = _str_protect($_POST['post_content']);
            $_POST['post_title'] = _str_protect($_POST['post_title']);
            $exec = $pdo->prepare($sql);
            $exec->bindValue(':post_content', $_POST['post_content']);
            $exec->bindValue(':post_title', $_POST['post_title']);
            $exec->bindValue(":{$user}", $_POST['post_author']);
            $exec->execute();
            if ($sql !== false) {
                echo 'Good, post have been added! ';
            }
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}
_db_connect('localhost', 'root', 'content', '');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../css/add_style.css">
    <script type="text/javascript" src="../js/script.js"></script>
    <title>Content</title>
</head>
<body>
<h2 class="title">
    You can add posts here --- <?php 
echo '<a href="http://' . $_SERVER['HTTP_HOST'] . '/index.php">Go to see posts</a>';
?>
    <span class="user" style="float:right"><?php 
示例#3
0
function conn()
{
    if (!isset($GLOBALS['conn']) || !is_resource($GLOBALS['conn'])) {
        _db_connect();
    }
    if (is_resource($GLOBALS['conn'])) {
        if (!$GLOBALS['conn']->ping()) {
            $GLOBALS['conn']->close();
            _db_connect();
        }
    }
    if (is_resource($GLOBALS['conn'])) {
        return $GLOBALS['conn'];
    }
    return false;
}
示例#4
0
	function _db_query($sql, $cluster, $k=null){

		$cluster_key = $k ? "{$cluster}-{$k}" : $cluster;

		if (!$GLOBALS['db_conns'][$cluster_key]){
			_db_connect($cluster, $k);
		}

		#
		# Used to see what function called do_query
		#
		$backtrace = debug_backtrace();
		array_shift($backtrace);
		$caller = array_shift($backtrace);

		$start = microtime_ms();
		$result = @mysql_query($sql . " /* " . $caller . " */", $GLOBALS['db_conns'][$cluster_key]);
		$end = microtime_ms();

		$GLOBALS['timings']['db_queries_count']++;
		$GLOBALS['timings']['db_queries_time'] += $end-$start;

		log_notice('db', "DB-$cluster_key: $sql", $end-$start);


		#
		# profiling?
		#

		$profile = null;

		if ($GLOBALS['cfg']['db_profiling']){
			$profile = array();
			$p_result = @mysql_query("SHOW PROFILE ALL", $GLOBALS['db_conns'][$cluster_key]);
			while ($p_row = mysql_fetch_array($p_result, MYSQL_ASSOC)){
				$profile[] = $p_row;
			}
		}


		#
		# build result
		#

		if (!$result){
			$error_msg	= mysql_error($GLOBALS['db_conns'][$cluster_key]);
			$error_code	= mysql_errno($GLOBALS['db_conns'][$cluster_key]);

			log_error("DB-$cluster_key: $error_code ".HtmlSpecialChars($error_msg));

			$ret = array(
				'ok'		=> 0,
				'error'		=> $error_msg,
				'error_code'	=> $error_code,
				'sql'		=> $sql,
				'cluster'	=> $cluster,
				'shard'		=> $k,
			);
		}else{
			$ret = array(
				'ok'		=> 1,
				'result'	=> $result,
				'sql'		=> $sql,
				'cluster'	=> $cluster,
				'shard'		=> $k,
			);
		}

		if ($profile) $ret['profile'] = $profile;

		return $ret;
	}