I'm trying out this simple code, made by myself from scratch, so I think I'm missing something here, because I get an error and don't know why.
I'm trying to log in the user. The script looks into the database and if it founds a result (there's only one user) it sets a session for that user.
This is the HTML log in form at login.php:
<form name="loguearse" action="" method="POST">
Usuario: <input type="text" name="usuario">
Clave: <input type="text" name="clave">
<input name="recordar" type="checkbox" value="1"/> recordar datos
<input type="submit" name="submit" value="Entrar!"> <a href="salir.php">Salir</a>
<br>
</form>
And here's the php code to handle the session. It's in the same file, right below the html code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$conexion=mysqli_connect('localhost','user','pass','db')
OR die('Oops! Error '.mysqli_connect_errno());
$consulta=' SELECT * FROM usuario
WHERE username="'.$_POST['usuario'].'"
AND pass="'.$_POST['clave'].'"
';
$resultado=mysqli_query($conexion,$consulta);
mysqli_close($conexion);
$rows = mysqli_num_rows($resultado);
if ($rows) {
$usuarioLogueado=$_SESSION['usuario'];
if ($_POST['recordar']) {
setcookie ("usuario",$usuarioLogueado, time()+24*60*60*30);
setcookie ("clave",$clave, time()+24*60*60*30);
} else {
setcookie("usuario","");
setcookie("clave","");
}
}
if (isset($usuarioLogueado)) {
echo 'Hola '.$usuarioLogueado. ' Session Id: '.session_id();
} else echo 'Nada';
}
I always get "Nada".
This is how I've set up the usuario table:
create table if not exists usuario (
uid int unsigned not null auto_increment primary key,
username char(20) not null,
pass char(20) not null,
name char(200) not null,
email char(200) not null
);
And this is the error I get at the logs:
[:error] [pid 2727] [client 127.0.0.1:44577] PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /var/www/html/demo/login.php on line 42, referer: http://localhost/demo/index.php
In case anyone wonders about index.php (because of what the error says):
<?php
session_start();
include 'login.php';
I know there probably are tons of other errors, these are just my first steps into programming, but I can't understand why mysqli_num_rows() gives me an error, I've constructed it following the docs example...