Acceso a Base de datos en PHP y ASP (parte 1)

Aqu铆 muestro como acceso a base de datos con PHP y ASP:

Base de datos: MySql
Codificaci贸n: utf-8

Supongamos que tenemos la tabla (Noticias)

tabla (Noticias)
IdNoticia (int)
Nombre (varchar)
fecha (datetime)

con los siguientes registros:

IdNoticia Nombre fecha
1 pepe 2004-08-03 (este es el formato en que Mysql guarda las fechas)
2 poncho 2008-12-28
3 pedro 2009-01-24


__________________________________________________________________
PHP:

<?php header(’Content-Type: text/html; charset=UTF-8′);
$noLimits=5;?> nota: esta variable la utilizo porque me gusta traer un m谩ximo de registros

<table id=’tablesorter’ class=’tablesorter’ border=’0′ cellspacing=’1′ width=’680′>
<thead>
<tr>
<th>Id Noticia</th>
<th>Nombre</th>
<th>fecha</th>
</tr>
</thead>
<tbody>
<?php $SQL=”SELECT IdNoticia,Nombre,date_format(fecha,’%d/%m/%Y’) FROM Noticias order by IdNoticia desc limit “.$noLimits;
$conexion=mysql_connect($DBserver,$DBuser,$DBpassword);
mysql_select_db($DBname,$conexion) or die(mysql_error());
msql_query(”SET NAMES ‘utf8′”); nota: Con esto indicamos la codificaci贸n de la informaci贸n
$tabla=mysql_query($SQL);
if (!$tabla) {
   echo ‘No se pudo ejecutar el query: ‘ . mysql_error();
   exit;
}
mysql_close($conexion);
$rows=mysql_num_rows($tabla);
if($rows> 0)
{
   $str0=”;
   while ($row = mysql_fetch_array($tabla))
   {
      $str0=$str0.”<tr>”;
      for( $J = 0;$J<3;$J++)
         $str0=$str0.”<td class=’datagrid-td’>”.$row[$J].”</td>”;
      $str0=$str0.”</tr>”;
      }
   mysql_free_result($tabla);
   echo $str0;
}else
   echo “<tr><td> </td><td>No existe ning煤n registro</td><td> </td></tr>”; ?>
</tbody>
________________________________________________________________
ASP:

<%@LANGUAGE=’VBSCRIPT’ CODEPAGE=’65001′%>
nota:CODEPAGE para utf-8 noLimits=5%>

<table id=’tablesorter’ class=’tablesorter’ border=’0′ cellspacing=’1′ width=’680′>
<thead>
<tr>
<th>Id Noticia</th>
<th>Nombre</th>
<th>fecha</th>
</tr>
</thead>
<tbody><%SQL = “SELECT IdNoticia,Nombre,date_format(fecha,’%d/%m/%Y’) FROM Noticias order by IdNoticia desc limit “&noLimits
set tabla = nothing
set Conn=Server.CreateObject(”ADODB.connection”)
Conn.Open(strCon)
set rs = Server.CreateObject(”ADODB.recordset”)

On error Resume Next
   rs.Open SQL, Conn
if Err<>0 then
   response.Write(Err& ” mensaje error “)
   rs.Close
   set rs = nothing
   Conn.Close
   set Conn = nothing
else
   tabla = rs.GetRows
   rs.Close
   Conn.Close
   Set Conn = Nothing
   str0=”"
   for I = 0 to UBound(tabla,2)
      str0=str0&”<tr>”
      for J = 0 to Ubound(tabla, 1)
         str0=str0&”<td>”&tabla(J, I)&”</td>”
      next
      str0=str0&”</tr>”
   next
   set tabla = nothing
end if
response.Write(str0)%>
</tbody>
____________________________________________
Resultado para ambos codigos:

IdNoticia Nombre fecha
3 pedro 01/24/2009
2 poncho 28/12/2008
1 pepe 03/08/2004

Continuar谩 con ingresar y modificar datos

Puedes seguir cualquier respuesta a esta entrada mediante el canal RSS 2.0. Puedes dejar un comentario o enviar un trackback desde tu propio sitio.

Dejar un comentario

Debes ingresar para dejar un comentario.