Hi guys!

I got a problem with my logging in system. I can connect to my database with the code:

session_start();
                        
                        include('header.html');
                        $page_title = 'Home Page';
                        
                        
                        $mail = $_POST['email'];
                        $password = $_POST['password'];
                        $msg ='';
                        
                        if(isset($mail,$password)){
                                $con = mysql_connect('127.0.0.1','root','');
                            
                                if(!$con)
                                {
                                    die('Could not connect: '.mysql_error());
                                }
                                else{
                                    echo "Connected!";
                                }
                        }       

$db_found = mysql_select_db("dbusers", $con);

Then I connect to the table and I want to retrieve the registered users details from the table "mytable" and then I want to greet the registered user with by his name.

$myMail = stripslashes($mail);
                            $mypassword = stripslashes($password);
                           
                            $sql ="SELECT * FROM mytable WHERE Email = '$myMail' AND Password='$mypassword'";
                            $result = mysql_query($sql);
                            
                            $sql2 ="SELECT Name FROM mytable WHERE Email = '$myMail' AND Password='$mypassword'";
$name = mysql_query($sql2);

No I run through the "array" to find the name according to the email and password

while($rows = mysql_fetch_row($result)){
                                if($rows['Name'] == $name ){
                                    echo "<h1>Welcome $name </h1>";
                                }
                                else{
                                    echo "No Name";
                                }
                            }

But the error states:

Notice: Undefined index: Name in C:\Program Files\EasyPHP-5.3.8.1\www\Login.php on line 46

Ok I understand it but the in my table in columns name is "Name" correctly spelled.

Its prob just something small...just need a fresh mind to help me :D

Sorry for the long post.

Kind Regards

Philip

Ok one thing I changed in the while loop the

while($rows = mysql_fetch_row($result)){

TO

while($rows = mysql_fetch_array($result)){

so the error is gone but still I can't retrieve the name of the database/table :/ it just gives me "No Name"

Yo philly!

try something like this:

$server = '127.0.0.1';
$user_name = 'root';
$password = '';
$database = 'yourdatabase name';

if(!mysql_connect('127.0.0.1','root',''))
{
   if(!mysql_connect('localhost', 'root', ''))
{
 	exit('Error: could not establish database connection');
}
if(!mysql_select_db($database))
{
 	exit('Error: could not select the database');
}

and remember to say

include "file.php"

in every file at the top in which there are contents that require a connection with your database.

This is now if you wish to connect to your database!

My connection works to my database! It prints the "Connected" string so that is fine...this is not the problem

I can't retrieve the data from the table...although the connection is made and correctly spelled

why use a loop to find the user info? why not just grab that specific user's data from the table in the first query

$result=mysql_query("SELECT * FROM mytable WHERE 
Email = '".mysql_real_escape_string($myMail)."' AND 
Password='".mysql_real_escape_string($mypassword)."'") or die(mysql_error());
if (mysql_num_rows($result)==0){echo "user not found";}
else{
   $user=mysql_fetch_assoc($result);
   echo $user['Name'];
}

why use a loop to find the user info? why not just grab that specific user's data from the table in the first query

$result=mysql_query("SELECT * FROM mytable WHERE 
Email = '".mysql_real_escape_string($myMail)."' AND 
Password='".mysql_real_escape_string($mypassword)."'") or die(mysql_error());
if (mysql_num_rows($result)==0){echo "user not found";}
else{
   $user=mysql_fetch_assoc($result);
   echo $user['Name'];
}

Thank you!It works!!Didn't really know about the mysql_fetch_assoc function but sweet! And so you learn!

@evstevemd - I checked it out-and it's definitely what I was looking for-just a lot of video's though- I'm capped with my BW :/ but non the less Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.