Jump to content

[SOLVED] displaying a single result from Array


jimmyt1988

Recommended Posts

This code checks if username and password are entered, if so it says Welcome (usersDetails).

 

I want it to say Welcome (firstName) and not display the other objects in the array.

 

How do i do this?:

 

    $con = mysql_connect($server, $username, $password);
    mysql_select_db('distal', $con);
   
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
       
    $username = $_POST['username'];  
    $password = $_POST['password'];    
       
    $query = mysql_query("SELECT * FROM userRegistration WHERE userName='$username' AND password='$password'");
    $result = mysql_num_rows($query);
    if (!$result){
        echo "incorrect username or password";
    }  
    else{
        echo "welcome ";
        while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){
            foreach($result as $userDetails){
                echo $userDetails . "<br />";
            } 
        }
    }

 

 

I have tried:

 

echo "welcome ";

        while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){

            foreach($result as $userDetails){

                echo $userDetails['firstName'] . "<br />";

            }

        }

 

and

 

echo "welcome ";

        while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){

            foreach($result as $userDetails['firstName']){

                echo $userDetails . "<br />";

            }

        }

 

and

 

echo "welcome ";

        while ($result = mysql_fetch_array($query,MYSQL_ASSOC)){

            foreach($result['firstName'] as $userDetails){

                echo $userDetails . "<br />";

            }

        }

Hi

 

Like this:-

 

    $con = mysql_connect($server, $username, $password);
    mysql_select_db('distal', $con);
   
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
       
    $username = $_POST['username'];  
    $password = $_POST['password'];    
       
    $query = mysql_query("SELECT * FROM userRegistration WHERE userName='$username' AND password='$password'");
    $result = mysql_num_rows($query);
    if (!$result){
        echo "incorrect username or password";
    }  
    else{
        echo "welcome ";
        if ($result = mysql_fetch_array($query)) echo $result['firstName'] . "<br />";
    }

 

All the best

 

Keith

thankyou very much.

 

Super helpful forum and people ! :D

 

is there a way to do this easier?

 

else{

        echo "welcome ";

        if ($result = mysql_fetch_array($query)){

            echo $result['firstName'] . " " .  $result['lastName'] . "<br />";

        }

    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.