Como migrar de Mysql a PDO
Publicado por Master (7 intervenciones) el 15/04/2017 18:05:35
Hey que tal amigos tengo una duda sobre algunas funciones de Mysqli que quiero migrar a PDO e intentado migar este código:
------------------------------------------------------------------------------------------------
Y el resultado de mi conversión es la siguiente:
------------------------------------------------------------------------------------------------
Pero no me muestra la información de la tabla usuario y cuando estaba en Mysql si lo cual me hace pensar que es un problema con las funciones espero puedan ayudarme!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
$con=@mysqli_connect('localhost', '', '', 'prueba');
if(!$con){
die("imposible conectarse: ".mysqli_error($con));
}
if (@mysqli_connect_errno()) {
die("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
}
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
include 'pagination.php'; //incluir el archivo de paginación
//las variables de paginación
$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
$per_page = 10; //la cantidad de registros que desea mostrar
$adjacents = 4; //brecha entre páginas después de varios adyacentes
$offset = ($page - 1) * $per_page;
//Cuenta el número total de filas de la tabla*/
$count_query = mysqli_query($con,"SELECT count(*) AS numrows FROM usuario ");
if ($row= mysqli_fetch_array($count_query)){$numrows = $row['numrows'];}
$total_pages = ceil($numrows/$per_page);
$reload = 'index.php';
//consulta principal para recuperar los datos
$query = mysqli_query($con,"SELECT * FROM usuario order by id LIMIT $offset,$per_page");
if ($numrows>0){
?>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Cédula</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['nombre'];?></td>
<td><?php echo $row['apellido'];?></td>
<td><?php echo $row['cedula'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<div class="table-pagination pull-right">
<?php echo paginate($reload, $page, $total_pages, $adjacents);?>
</div>
<?php
} else {
?>
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4>Aviso!!!</h4> No hay datos para mostrar
</div>
<?php
}
}
?>
Y el resultado de mi conversión es la siguiente:
------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
try{
$conn = new PDO('pgsql:host=localhost;port=5432;dbname=prueba', "prueba","1234" );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo "ERROR: " . $e->getMessage();
}
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
include 'pagination.php'; //incluir el archivo de paginación
//las variables de paginación
$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
$per_page = 10; //la cantidad de registros que desea mostrar
$adjacents = 4; //brecha entre páginas después de varios adyacentes
$offset = ($page - 1) * $per_page;
//Cuenta el número total de filas de la tabla
$sql = $conn->prepare("SELECT count(*) AS numrows FROM usuario");
if ($row= $sql->fetchAll()){$numrows = $row['numrows'];}
$total_pages = ceil($numrows/$per_page);
$reload = 'index.php';
//consulta principal para recuperar los datos
$query = $conn->prepare("SELECT * FROM usuario order by id LIMIT $offset,$per_page");
if ($numrows>0){
?>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Cédula</th>
</tr>
</thead>
<tbody>
<?php
while($row = $query->fetchAll()){
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['nombre'];?></td>
<td><?php echo $row['apellido'];?></td>
<td><?php echo $row['cedula'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<div class="table-pagination pull-right">
<?php echo paginate($reload, $page, $total_pages, $adjacents);?>
</div>
<?php
} else {
?>
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4>Aviso!!!</h4> No hay datos para mostrar
</div>
<?php
}
}
?>
Valora esta pregunta
0