Skip to content

Instantly share code, notes, and snippets.

@everaldo
Created May 9, 2015 00:02
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 everaldo/ea6fd3a34a5f0ec3785c to your computer and use it in GitHub Desktop.
Save everaldo/ea6fd3a34a5f0ec3785c to your computer and use it in GitHub Desktop.
contador, segunda versão
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Contador de Visitas</title>
</head>
<body>
<?php
function imprime_num_visitantes($visitas){
return "<h1>" . $visitas . "</h1>";
}
function get_num_visitantes(){
//conectar
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Não foi possível conectar: ' . mysql_error());
}
mysql_select_db("contador");
$query = "UPDATE `visitas` " .
"SET total = total + 1";
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$query = "SELECT total " .
"FROM `visitas` " .
"ORDER BY total ASC LIMIT 1";
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$row = mysql_fetch_array($result);
mysql_close($link);
return $row["total"];
}
$contador = get_num_visitantes();
echo imprime_num_visitantes($contador);
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment