Foros del Web » Programando para Internet » PHP »

php para consultas

Estas en el tema de php para consultas en el foro de PHP en Foros del Web. Hola a todos: Intento realizar una consulta sobre una base de datos MYSQL (llamada contactos), que tiene varios campos, pero me interesa que la consulta ...
  #1 (permalink)  
Antiguo 11/11/2010, 03:45
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
php para consultas

Hola a todos:
Intento realizar una consulta sobre una base de datos MYSQL (llamada contactos), que tiene varios campos, pero me interesa que la consulta se realice sobre 3 campos ciudad, tituloanuncio, descripcionanuncio, pero me arroja el siguiente error.
Query was empty
Alguna sugerencia, ? o código alternativo para realizar esta consulta?

Gracias.

Rafa.

Código PHP:
Ver original
  1. <?php require_once('Connections/contactos.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. mysql_select_db($database_contactos, $contactos);
  35. $query_Recordset1 = "SELECT ciudad, tituloanuncio, descripcionanuncio FROM contactos WHERE ciudad LIKE CONCAT("%", TRIM(IF(LENGTH(TRIM($ciud)) > 0, $ciud, ciudad)), "%") AND tituloanuncio LIKE CONCAT("%", TRIM(IF(LENGTH(TRIM($titul)) > 0, $titul, tituloanuncio)), "%") AND descripcionanuncio LIKE CONCAT("%", TRIM(IF(LENGTH(TRIM($descr)) > 0, $descr, descripcionanuncio)), "%")  ";
  36. $Recordset1 = mysql_query($query_Recordset1, $contactos) or die(mysql_error());
  37. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  38. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  39. ?>
  #2 (permalink)  
Antiguo 11/11/2010, 04:22
 
Fecha de Ingreso: marzo-2010
Ubicación: Barcelona
Mensajes: 657
Antigüedad: 14 años, 1 mes
Puntos: 26
Respuesta: php para consultas

Antes de hacer el mysql_query() haz un echo $query_Recordset1; y a ver si la sentencia que sale es correcta.
  #3 (permalink)  
Antiguo 11/11/2010, 04:30
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
Respuesta: php para consultas

El código que muestro, lo realice con Dreamweaver.
Soy bastante novato en el tema.
¿? $query_Recordset1
Me interesaría saber, si existe algún código que se pueda adaptar a la búsqueda que me interesa.

Gracias.
  #4 (permalink)  
Antiguo 11/11/2010, 04:43
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
Respuesta: php para consultas

Modificandolo me da error:

Parse error: syntax error, unexpected T_ECHO in /var/www/vhost/.../html/buscar por palabra clave_ejemplo.php on line 36

Con echo $query_Recordset1

Código PHP:
Ver original
  1. <?php require_once('Connections/contactos.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. mysql_select_db($database_contactos, $contactos);
  35. $query_Recordset1 = "SELECT ciudad, tituloanuncio, descripcionanuncio FROM contactos WHERE ciudad LIKE CONCAT("%", TRIM(IF(LENGTH(TRIM($ciud)) > 0, $ciud, ciudad)), "%") AND tituloanuncio LIKE CONCAT("%", TRIM(IF(LENGTH(TRIM($titul)) > 0, $titul, tituloanuncio)), "%") AND descripcionanuncio LIKE CONCAT("%", TRIM(IF(LENGTH(TRIM($descr)) > 0, $descr, descripcionanuncio)), "%")  ";
  36. $Recordset1 = echo $query_Recordset1($query_Recordset1, $contactos) or die(mysql_error());
  37. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  38. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  39. ?>
  #5 (permalink)  
Antiguo 11/11/2010, 04:46
 
Fecha de Ingreso: marzo-2010
Ubicación: Barcelona
Mensajes: 657
Antigüedad: 14 años, 1 mes
Puntos: 26
Respuesta: php para consultas

Te da error al hacer un echo $query_Recordset1; ???? Has puesto bien el echo?

De todas formas prueba esta consulta:

Código PHP:
$query_Recordset1 "SELECT ciudad, tituloanuncio, descripcionanuncio FROM contactos WHERE ciudad LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM($ciud)) > 0, $ciud, ciudad)), '%') AND tituloanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM($titul)) > 0, $titul, tituloanuncio)), '%') AND descripcionanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM($descr)) > 0, $descr, descripcionanuncio)), '%')  "
A ver si era asunto de las comillas o no...
  #6 (permalink)  
Antiguo 11/11/2010, 06:14
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
Respuesta: php para consultas

Error.

Parse error: syntax error, unexpected T_ECHO in /var/www/vhost/.../home/html/buscar por palabra clave_ejemplo.php on line 36
  #7 (permalink)  
Antiguo 11/11/2010, 06:29
 
Fecha de Ingreso: marzo-2010
Ubicación: Barcelona
Mensajes: 657
Antigüedad: 14 años, 1 mes
Puntos: 26
Respuesta: php para consultas

Me puedes mostrar el codigo donde has puesto el echo?
Has probado lo de las comillas de la sentencia sql?
  #8 (permalink)  
Antiguo 11/11/2010, 07:12
 
Fecha de Ingreso: febrero-2010
Mensajes: 818
Antigüedad: 14 años, 2 meses
Puntos: 55
Respuesta: php para consultas

las variables dentro de las consultas tienen que ir con comillas simples al igual que los %
  #9 (permalink)  
Antiguo 11/11/2010, 07:13
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
Respuesta: php para consultas

perdona..

Código PHP:
Ver original
  1. <?php require_once('Connections/contactos.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. mysql_select_db($database_contactos, $contactos);
  35. $query_Recordset1 = "SELECT ciudad, tituloanuncio, descripcionanuncio FROM contactos WHERE ciudad LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM($ciud)) > 0, $ciud, ciudad)), '%') AND tituloanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM($titul)) > 0, $titul, tituloanuncio)), '%') AND descripcionanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM($descr)) > 0, $descr, descripcionanuncio)), '%')  ";
  36. $Recordset1 = echo $query_Recordset1($query_Recordset1, $contactos) or die(mysql_error());
  37. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  38. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  39. ?>
  #10 (permalink)  
Antiguo 11/11/2010, 07:16
 
Fecha de Ingreso: febrero-2010
Mensajes: 818
Antigüedad: 14 años, 2 meses
Puntos: 55
Respuesta: php para consultas

modifica las comillas y el echo
Código PHP:
#
mysql_select_db($database_contactos$contactos);
#
$query_Recordset1 "SELECT ciudad, tituloanuncio, descripcionanuncio FROM contactos WHERE ciudad LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('$ciud')) > 0, '$ciud', ciudad)), '%') AND tituloanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('$titul')) > 0, '$titul', tituloanuncio)), '%') AND descripcionanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('$descr')) > 0, '$descr', descripcionanuncio)), '%')  ";
#
echo $query_Recordset1//para ver consulta por pantalla
#
$Recordset1 mysql_query($query_Recordset1$contactos) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
#
$totalRows_Recordset1 mysql_num_rows($Recordset1); 
  #11 (permalink)  
Antiguo 11/11/2010, 07:44
 
Fecha de Ingreso: julio-2010
Ubicación: Cerca Sitges (Barcelona)
Mensajes: 98
Antigüedad: 13 años, 9 meses
Puntos: 4
Respuesta: php para consultas

Me muestra la página ok, pero en la parte superior de la pantalla me aparece:

SELECT ciudad, tituloanuncio, descripcionanuncio FROM contactos WHERE ciudad LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('')) > 0, '', ciudad)), '%') AND tituloanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('')) > 0, '', tituloanuncio)), '%') AND descripcionanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('')) > 0, '', descripcionanuncio)), '%')

con el código:
Código PHP:
Ver original
  1. <?php require_once('Connections/contactos.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. mysql_select_db($database_contactos, $contactos);
  35. $query_Recordset1 = "SELECT ciudad, tituloanuncio, descripcionanuncio FROM contactos WHERE ciudad LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('$ciud')) > 0, '$ciud', ciudad)), '%') AND tituloanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('$titul')) > 0, '$titul', tituloanuncio)), '%') AND descripcionanuncio LIKE CONCAT('%', TRIM(IF(LENGTH(TRIM('$descr')) > 0, '$descr', descripcionanuncio)), '%')  ";
  36. echo $query_Recordset1; //para ver consulta por pantalla
  37. $Recordset1 = mysql_query($query_Recordset1, $contactos) or die(mysql_error());
  38. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  39. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  40. ?>
  #12 (permalink)  
Antiguo 12/11/2010, 03:35
 
Fecha de Ingreso: marzo-2010
Ubicación: Barcelona
Mensajes: 657
Antigüedad: 14 años, 1 mes
Puntos: 26
Respuesta: php para consultas

Eso es el echo que hemos creado para visualizar la consulta, ya lo puedes borrar, y así no te saldrá eso en la pagina.

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:09.