David Rodrigues February 2016

Issue while trying to read Table - MySQL

I believe that this is a really basic problem but i am starting my coding at PHP so i really can't do it all alone:

I've been for 2 days trying to solve a strange problem. I am using XAMPP for my database and its tables with the objective of creating a login and register system.

This is my user table:

CREATE TABLE `playware`.`users` (
`user_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 25 ) NOT NULL ,
`surname` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 35 ) NOT NULL ,
`telephone` VARCHAR( 9 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
UNIQUE (`email`)
) ENGINE = MYISAM ;

The register .php file is working (it's adding the user to the table correctly) but when i try to make login i just can't.

So I've made this on my login.php file just to test if it gets the row:

<?php
include_once 'connect.php';
session_start();
if(isset($_SESSION['user'])!="")
{
    header("Location: index_home.php");
}
$email = $_POST["email"];
$upass = $_POST["password"];
$sql="SELECT email FROM users";
$res=$conn->query($sql);
if($res->num_rows>0)
{
    while($row=$res->fetch_assoc());
    {
        if($row["email"]==$email)
        {
            echo"Writting email: ".$row["email"]." - done writting.";
        }
    }
}
?>

All time i try to do this results on a white screen, nothing more, when it's supposed to appear the user email. Someone knows how to fix this?

================================UPDATE================================== The error was:

while($row=$res->fetch_assoc());
{
    //Stuff here ...
}

It can't take the ";" there because it cancels the outputting. So the fix is:

while($row=$res->fetch_assoc())
(...)

As I expected, it was a basic error ... Thank you for people that supported me.

Answers


Jay Blanchard February 2016

You have an error here:

if(isset($_SESSION['user'])!="")

You cannot check if a variable is set and not equal to blank in this way. Instead you should do this:

if(isset($_SESSION['user']) && $_SESSION['user']!="")

You have another error here, which stops the script from outputting anything:

while($row=$res->fetch_assoc());
---------remove the -----------^

Since that is valid syntax you may not have seen anything in error reporting.


You really should use PHP's built-in functions to handle password security. If you're using a PHP version less than 5.5 you can use the password_hash() compatibility pack.

In addition make sure you're using prepared statements for PDO and/or MySQLi and consider using PDO, it's really pretty easy.

Post Status

Asked in February 2016
Viewed 1,324 times
Voted 12
Answered 1 times

Search




Leave an answer