Foros del Web » Programando para Internet » PHP »

Consulta de Base de datos MYSQL y PHP

Estas en el tema de Consulta de Base de datos MYSQL y PHP en el foro de PHP en Foros del Web. hola espero que alguien me pueda ayudar estoy armando un portal de noticias y necesito hacer una consulta entre las tablas Noticias y Multimedia, porque ...
  #1 (permalink)  
Antiguo 04/04/2009, 22:09
 
Fecha de Ingreso: noviembre-2003
Mensajes: 20
Antigüedad: 20 años, 5 meses
Puntos: 3
Consulta de Base de datos MYSQL y PHP

hola espero que alguien me pueda ayudar estoy armando un portal de noticias y necesito hacer una consulta entre las tablas Noticias y Multimedia, porque en la tabla mutimedia voy a gardar las fotos, audios y videos de cada noticias y como pude haber mas de una foto o audio o video o puede tener las tres cosa yo quiero seleccionarlo por el campo Idnoticia.

en la tabla noticias tengo los siguientes campos

Idnoticia, fecha, tipo, titulo, bajada, texto, prioridad

en la tabla multimedia tengo los siguientes campos

idmultimedia, idnoticia, tipo, archivo, prioridad


y mi problema es que nose como hacer consultas entre dos tablas espero que alguien me pueda ayudar, se lo agradecere mucho. jajajaja
  #2 (permalink)  
Antiguo 04/04/2009, 22:14
 
Fecha de Ingreso: julio-2008
Mensajes: 140
Antigüedad: 15 años, 8 meses
Puntos: 2
Respuesta: Consulta de Base de datos MYSQL y PHP

Sencillo, en la consulta el parametro WHERE tiene que ser idnoticia=(numero).

O sea primero buscas las noticias, una vez que encontraste dicha noticia, buscas la parte multimedia de esa noticia.
  #3 (permalink)  
Antiguo 04/04/2009, 22:19
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Consulta de Base de datos MYSQL y PHP

Lo puedes hacer con join

Código mysql:
Ver original
  1. select idnoticia,fecha,tipo,archivo, prioridad from noticias inner join multimedia on noticias.idnoticia = multimedia.idnoticias where 'condicion';
De esta forma, puedes seleccionar los campos que quieras mostrar de las tablas.

Un saludo
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #4 (permalink)  
Antiguo 04/04/2009, 23:00
 
Fecha de Ingreso: noviembre-2003
Mensajes: 20
Antigüedad: 20 años, 5 meses
Puntos: 3
Respuesta: Consulta de Base de datos MYSQL y PHP

yo consulto la noticia de esta forma

Cita:
"SELECT * FROM noticias WHERE fecha <= '$mysql_date' AND tipo = 'PRINCIPAL' AND Seccion <> 'Deportes' AND seccion <> 'Tecnologia' ORDER BY fecha DESC, prioridad DESC, hora DESC "
ahora necesito seleccionear la foto de la la tabla multimedia cuyos campos sosn los siguientes:
id_multimedia, Id_noticia, Tipo, Archivo, Priorioridad.

hay forma de hacer una sola consulta o tengo que hacerlo en dos consultas distintas???
  #5 (permalink)  
Antiguo 05/04/2009, 05:35
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Consulta de Base de datos MYSQL y PHP

ey claute..

La respuesta te la dije en el post pasado.
con join pegas dos tablas y seleccionas los campos de las dos tablas que quieras en una sola consulta.
Si quieres todos los campos de noticias y todos los campos de multimedia para una misma noticia sería asi:

Código sql:
Ver original
  1. SELECT noticias.*,multimedia.* FROM noticias INNER JOIN multimedia ON noticias.idnoticia = multimedia.idnoticias WHERE noticias.fecha <= '$mysql_date' AND noticias.tipo = 'PRINCIPAL' AND noticias.Seccion <> 'Deportes' AND noticias.seccion <> 'Tecnologia' ORDER BY noticias.fecha DESC, noticias.prioridad DESC, noticias.hora DESC "

Un saludo.
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #6 (permalink)  
Antiguo 05/04/2009, 08:16
 
Fecha de Ingreso: noviembre-2003
Mensajes: 20
Antigüedad: 20 años, 5 meses
Puntos: 3
Respuesta: Consulta de Base de datos MYSQL y PHP

aca les pongo todo el código porque no me funciona.


Código:
<?php require_once('Connections/lrn.php'); ?>
<?php
$mysql_date = date("Y-m-d"); 
$mysql_time = date("H:i:s");

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}

$maxRows_RS_noticias_1 = 4;
$pageNum_RS_noticias_1 = 0;
if (isset($_GET['pageNum_RS_noticias_1'])) {
  $pageNum_RS_noticias_1 = $_GET['pageNum_RS_noticias_1'];
}
$startRow_RS_noticias_1 = $pageNum_RS_noticias_1 * $maxRows_RS_noticias_1;

$colname_RS_noticias_1 = "-1";
if (isset($_GET['fecha'])) {
  $colname_RS_noticias_1 = $_GET['fecha'];
}
mysql_select_db($database_lrn, $lrn);
$query_RS_noticias_1 = sprintf("SELECT noticias.*,multimedia.* FROM noticias INNER JOIN multimedia ON noticias.id_noticia = multimedia.id_noticia WHERE noticias.fecha <= '$mysql_date' AND noticias.tipo = 'PRINCIPAL' AND noticias.Seccion <> 'Deportes' AND noticias.seccion <> 'Tecnologia' ORDER BY noticias.fecha DESC, noticias.prioridad DESC, noticias.hora DESC ", GetSQLValueString($colname_RS_noticias_1, "date"));
$query_limit_RS_noticias_1 = sprintf("%s LIMIT %d, %d", $query_RS_noticias_1, $startRow_RS_noticias_1, $maxRows_RS_noticias_1);
$RS_noticias_1 = mysql_query($query_limit_RS_noticias_1, $lrn) or die(mysql_error());
$row_RS_noticias_1 = mysql_fetch_assoc($RS_noticias_1);

if (isset($_GET['totalRows_RS_noticias_1'])) {
  $totalRows_RS_noticias_1 = $_GET['totalRows_RS_noticias_1'];
} else {
  $all_RS_noticias_1 = mysql_query($query_RS_noticias_1);
  $totalRows_RS_noticias_1 = mysql_num_rows($all_RS_noticias_1);
}
$totalPages_RS_noticias_1 = ceil($totalRows_RS_noticias_1/$maxRows_RS_noticias_1)-1;
?><!-- NOTICIA NUMERO 1 -->
<link href="../estilos/noticias.css" rel="stylesheet" type="text/css" />

<table width="425" border="0">
  <?php do { ?>
    <tr>
      <td><table border="0" cellpadding="0" cellspacing="0" width="425">
        <tbody>
          <tr>
            <td width="25" valign="top"><img src="img/check2.gif" align="top"></td>
            <td width="417" class="tahoma t11 c10 tdnone"><span class="fecha_home"><?php echo makeDateTime($row_RS_noticias_1['fecha'], 'd-m-y'); ?> - <strong><?php echo $row_RS_noticias_1['seccion']; ?></strong></span><strong><br />
                <span class="copete_home"><?php echo $row_RS_noticias_1['copete']; ?></span></strong></td>
          </tr>
          <tr>
            <td colspan="2" height="4"></td>
            </tr>
          <tr>
            <td colspan="2" class="titulo"><a href="noticia.php?id_noticia=<?php echo $row_RS_noticias_1['id_noticia']; ?>" class="titulo"><?php echo $row_RS_noticias_1['titulo']; ?></a></td>
          </tr>
          <tr>
            <td colspan="2" height="4"></td>
            </tr>
          <tr>
            <td colspan="2"><?php if ($row_RS_noticias_1['foto1'] <> "") { // Show if recordset not empty ?>
                  <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#666666">
                    <tr>
                      <td><img src="fotos/<?php echo $row_RS_noticias_1['foto1']; ?>" width="423" /></td>
                    </tr>
                                        </table>
                  <?php } // Show if recordset not empty ?>                </td>
          </tr>
          <tr>
            <td colspan="2" height="10"></td>
            </tr>
          <tr>
            <td colspan="2" class="bajada"><?php echo nl2br($row_RS_noticias_1['bajada']); ?></td>
          </tr>
          <tr>
            <td colspan="2" height="10"></td>
            </tr>
          <tr>
            <td colspan="2" background="img/fondo_raya.gif" height="1"></td>
            </tr>
          <tr>
            <td colspan="2" height="10"></td>
            </tr>
        </tbody>
      </table></td>
    </tr>
    <?php } while ($row_RS_noticias_1 = mysql_fetch_assoc($RS_noticias_1)); ?>
</table>
<!-- FIN NOTICIA NUMERO 1 -->
<?php
mysql_free_result($RS_noticias_1);
?>
  #7 (permalink)  
Antiguo 05/04/2009, 08:40
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: Consulta de Base de datos MYSQL y PHP

Prueba la consulta desde la consola de mysql.
Si es la salida que deseas, pregunta en el foro de php donde puede estar el error.

Si el error lo tira la base de datos, comentalo otra vez aca. Por el momento veo que la consulta está bien.

Un saludo claute
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #8 (permalink)  
Antiguo 05/04/2009, 11:32
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Respuesta: Consulta de Base de datos MYSQL y PHP

Tema trasladado desde MySQL

http://www.forosdelweb.com/f21/funci...-datos-413499/
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 14:33.