problema consulta a base de datos
Publicado por Nico (6 intervenciones) el 07/12/2019 02:07:21
Hola alguies podría decirme porque el codigo del archivo que solo contiene codigo php funcion y el otro no?
Este funciona y encuentra al usuario
Este no encuentra al usuario
Este funciona y encuentra al usuario
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
<?php
$NOMBRE = 'JAIME';
$CLAVE = '333333';
echo 'mi codigo es:'.$NOMBRE;
if ($NOMBRE == '' && $CLAVE == '') {
// En caso contrario redirigimos el visitante a la página de inicio
header('Location: login.php');
//echo '<br><br>devolución login';
die();
} else {
echo 'entra en if 1';
/* * * mysql hostname ** */
$hostname = 'localhost';
/* * * mysql username ** */
$username = 'root';
/* * * mysql password ** */
$password = 'rootroot';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=dbtext", $username, $password);
/* * * echo a message saying we have connected ** */
echo 'Connected to database<br />';
/* * * set the error reporting attribute ** */
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* * * prepare the SQL statement ** */
$stmt = $dbh->prepare("SELECT * FROM clientes WHERE nombre = :nombre AND clave = :clave");
/*** bind the paramaters ***/
//echo 'mi codigo es:'.$NOMBRE;
$stmt->bindParam(':nombre', $NOMBRE, PDO::PARAM_STR);
$stmt->bindParam(':clave', $CLAVE, PDO::PARAM_INT);
/* * * execute the prepared statement ** */
$stmt->execute();
var_dump($stmt);
//echo ' stmt aquí '.$stmt;
// $row = $stmt->fetch();
//echo 'nombre: '. $row['nombre'].'<br>';
/* * * loop over the results
while($row = $stmt->fetch())
{
echo $row['animal_id'].' ';
echo $row['animal_type'].' ';
echo $row['animal_name']. '<br>';
}
while($row = $stmt->fetch())
{
echo $row['NOMBRE'].' ';
echo $row['CLAVE'].' ';
}
* ** */
if ($row = $stmt->fetch()) {
var_dump($row);
echo 'usuario registrado '.$row['NOMBRE'];
} else {
echo 'no está registrado'.$row['NOMBRE'];
}
// var_dump($row);
/* * * close the database connection ** */
$dbh = null;
} catch (PDOException $e) {
//no mostrar este mensaje, que de información al usuario del error
//crear un propio
echo $e->getMessage();
}
}
?>
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login2</title>
<table border="2" cellpadding="0" cellspacing="0"
style="border-collapse: collapse" bordercolor="#A9A0BA"
bgcolor="#EFEFEF">
<tr>
<td width="30%"><img border="0" src="logo.gif" width="278"
height="63"></td>
<td width="53%" align="center"><b> <font face="Batang"
size="4" color="#000080">Todo lo que necesita y más aquí lo
podrá encontrar</font></b></td>
</tr>
</table>
</head>
<?php
$NOMBRE = $_REQUEST['codigo'];
$CLAVE = $_REQUEST['contrasena'];
echo 'mi codigo es:'.$NOMBRE;
if ($NOMBRE == '' && $CLAVE == '') {
// En caso contrario redirigimos el visitante a la página de inicio
header('Location: login.php');
//echo '<br><br>devolución login';
die();
} else {
echo 'entra en if 1';
/* * * mysql hostname ** */
$hostname = 'localhost';
/* * * mysql username ** */
$username = 'root';
/* * * mysql password ** */
$password = 'rootroot';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=dbtext", $username, $password);
/* * * echo a message saying we have connected ** */
echo 'Connected to database<br />';
/* * * set the error reporting attribute ** */
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* * * prepare the SQL statement ** */
$stmt = $dbh->prepare("SELECT * FROM clientes WHERE nombre = :nombre AND clave = :clave");
/*** bind the paramaters ***/
//echo 'mi codigo es:'.$NOMBRE;
$stmt->bindParam(':nombre', $NOMBRE, PDO::PARAM_STR);
$stmt->bindParam(':clave', $CLAVE, PDO::PARAM_INT);
/* * * execute the prepared statement ** */
$stmt->execute();
var_dump($stmt);
//echo ' stmt aquí '.$stmt;
// $row = $stmt->fetch();
//echo 'nombre: '. $row['nombre'].'<br>';
/* * * loop over the results
while($row = $stmt->fetch())
{
echo $row['NOMBRE'].' ';
echo $row['CLAVE'].' ';
}
** */
if ($row = $stmt->fetch()) {
var_dump($row);
echo 'usuario registrado '.$row['NOMBRE'];
} else {
var_dump($row);
echo 'no está registrado'.$row['NOMBRE'];
echo 'no está registrado'.$NOMBRE;
}
//var_dump($row);
/* * * close the database connection ** */
$dbh = null;
} catch (PDOException $e) {
//no mostrar este mensaje, que de información al usuario del error
//crear un propio
echo $e->getMessage();
}
}
?>
<body>
</body>
</html>
Valora esta pregunta


0