-
Notifications
You must be signed in to change notification settings - Fork 73
/
ejemplo6PDO.php
125 lines (83 loc) · 4.04 KB
/
ejemplo6PDO.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<head>
<title> Ejempos PHP</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="estilo.css">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Ejemplos de PHP</h1>
</div>
<div class="jumbotron">
<h3 class="text-info"> fetch((PDO::FETCH_CLASS, "cd") , fetchObject('cd')</h3>
<div class="well well-sm text-info">
class cd
{<br>
public $titulo;<br>
public $cantante;<br>
public $año;<br>
public function mostrarDatos()<br>
{return "Metodo mostar:".$this->titulo." ".$this->cantante." ".$this->año;}
}
<br><br> $arrayResultado= $sql->fetchAll(PDO::FETCH_CLASS, "cd");<br>
foreach ($arrayResultado as $cdRecorrido) {<br>
echo $cdRecorrido->mostrarDatos();
<br>
}
<br> <br>
while( $cd= $sql->fetchObject('cd'))
{ <br>
print_r($cd->titulo); <br>
print_r($cd->cantante); <br>
print_r($cd->mostrarDatos());<br>
}
</div>
</div>
<?php
class cd
{
public $titulo;
public $cantante;
public $año;
public function mostrarDatos()
{return "Metodo mostar:".$this->titulo." ".$this->cantante." ".$this->año;}
}
try
{
$db = new PDO('mysql:host=localhost;dbname=cdcol;charset=utf8', 'root', '', array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$sql = $db->query('SELECT titel as titulo,interpret as cantante, jahr as año FROM cds ');
$catidadFilas = $sql->rowCount();
echo "cantidad de filas: ".$catidadFilas."<br>";
echo "<h3> fetchAll(PDO::FETCH_CLASS, 'cd');</h3></br>";
$arrayResultado= $sql->fetchAll(PDO::FETCH_CLASS, "cd");
foreach ($arrayResultado as $cdRecorrido) {
echo $cdRecorrido->mostrarDatos();
echo "<br>";
}
echo "<h3> fetchObject('cd')</h3></br>";
$sql = $db->query('SELECT titel as titulo,interpret as cantante, jahr as año FROM cds ');
while( $cd= $sql->fetchObject('cd'))
{
print_r($cd->titulo);
print_r($cd->cantante);
print_r($cd->mostrarDatos());
print("<br>\n");
}
} catch(PDOException $ex)
{
echo "error ocurrido!"; //user friendly message
echo $ex->getMessage();
}
?>
<a class="btn btn-info" href="indexPDO.html">Menu principal</a>
</div>
</body>
</html>