Created
May 9, 2015 00:02
-
-
Save everaldo/ea6fd3a34a5f0ec3785c to your computer and use it in GitHub Desktop.
contador, segunda versão
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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