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 />";

            }

        }

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 />";

        }

    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.