Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php require_once('Connections/db.php'); ?>
- <?php
- if (!function_exists("GetSQLValueString")) {
- function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
- {
- if (PHP_VERSION < 6) {
- $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
- }
- $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
- switch ($theType) {
- case "text":
- $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
- break;
- case "long":
- case "int":
- $theValue = ($theValue != "") ? intval($theValue) : "NULL";
- break;
- case "double":
- $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
- break;
- case "date":
- $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
- break;
- case "defined":
- $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
- break;
- }
- return $theValue;
- }
- }
- $colname_ceps = "-1";
- if (isset($_GET['cep'])) {
- $colname_ceps = $_GET['cep'];
- }
- $row_ceps = array();
- mysql_select_db($database_db, $db);
- $query_ceps = sprintf("SELECT
- (SELECT c.nome FROM cidades c WHERE c.id = cidade_id) AS cidade,
- (SELECT b.nome FROM bairros b WHERE b.id = bairro_id) AS bairro,
- cep,
- (SELECT uf.uf FROM estados uf WHERE uf.id = uf_cod) AS estado,
- logracompl
- FROM enderecos WHERE cep = %s", GetSQLValueString($colname_ceps, "text"));
- $ceps = mysql_query($query_ceps, $db) or die(mysql_error());
- $row_ceps = mysql_fetch_assoc($ceps);
- $totalRows_ceps = mysql_num_rows($ceps);
- if ($totalRows_ceps > 0){
- $cep_res[] = array(
- 'cep' => $row_ceps['cep'],
- 'estado' => $row_ceps['estado'],
- 'cidade' => utf8_encode($row_ceps['cidade']),
- 'endereco' => utf8_encode($row_ceps['logracompl']),
- 'bairro' => utf8_encode($row_ceps['bairro']));
- echo (json_encode($cep_res));
- } else {
- $query_ceps = sprintf("SELECT c.nome AS cidade,
- (SELECT uf.uf FROM estados uf WHERE uf.id = c.estado_cod) AS estado,
- c.cep
- FROM cidades c
- WHERE c.cep = %s", GetSQLValueString($colname_ceps, "text"));
- $ceps = mysql_query($query_ceps, $db) or die(mysql_error());
- $row_ceps = mysql_fetch_assoc($ceps);
- $totalRows_ceps = mysql_num_rows($ceps);
- if ($totalRows_ceps > 0){
- $cep_res[] = array(
- 'cep' => $row_ceps['cep'],
- 'estado' => $row_ceps['estado'],
- 'cidade' => utf8_encode($row_ceps['cidade']));
- echo (json_encode($cep_res));
- } else {
- $cep_res[] = array();
- echo (json_encode($cep_res));
- }
- }
- mysql_free_result($ceps);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement