Select data from a row in database












-1















I am making a profile page in which I want the user's email to be displayed. I thought this would be quite a simple code that could be achieved using the select function from the database. However, this only works for one string and I cannot seem to figure out why.



This is my original code



session_start();

$_SESSION["user"] = $username;
$_SESSION["pass"] = $password;
$_SESSION["email"] = $email;
$connection = mysqli_connect ("localhost", "root", "", "picshare");

if ($connection ->connect_error) {
die("Connection failed: " . $connection->connect_error);
}else{

$query = mysqli_query($connection, "SELECT email FROM login WHERE username='".$_SESSION["user"]."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);


}else{
echo "error";
$conn->close();

}}


When I try and echo $field, nothing was echoed



            <p class ="right uc"><?php       echo($field['email']);?></p> 


I retried the code, but instead of using a session, I made a variable



$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "picshare";
$user = 'Eniola Olaogun';

$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{

$query = mysqli_query($conn, "SELECT email FROM login WHERE username='".$user."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);
echo($field['email']);

}else{
echo "error";
$conn->close();


This code displayed the email, and so I proceeded to change the $user variable to another name and the original problem occurred where nothing was echoed.



I went back to the original code and I logged in as Eniola Olaogun and the email was echoed, but as soon as I changed the person I logged in as, no email was echoed.



I am not sure why I am experiencing this problem and some help would be greatly appreciated










share|improve this question




















  • 1





    mysqli_error($con) that won't work, you're using a different variable.

    – Funk Forty Niner
    Nov 24 '18 at 17:30
















-1















I am making a profile page in which I want the user's email to be displayed. I thought this would be quite a simple code that could be achieved using the select function from the database. However, this only works for one string and I cannot seem to figure out why.



This is my original code



session_start();

$_SESSION["user"] = $username;
$_SESSION["pass"] = $password;
$_SESSION["email"] = $email;
$connection = mysqli_connect ("localhost", "root", "", "picshare");

if ($connection ->connect_error) {
die("Connection failed: " . $connection->connect_error);
}else{

$query = mysqli_query($connection, "SELECT email FROM login WHERE username='".$_SESSION["user"]."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);


}else{
echo "error";
$conn->close();

}}


When I try and echo $field, nothing was echoed



            <p class ="right uc"><?php       echo($field['email']);?></p> 


I retried the code, but instead of using a session, I made a variable



$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "picshare";
$user = 'Eniola Olaogun';

$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{

$query = mysqli_query($conn, "SELECT email FROM login WHERE username='".$user."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);
echo($field['email']);

}else{
echo "error";
$conn->close();


This code displayed the email, and so I proceeded to change the $user variable to another name and the original problem occurred where nothing was echoed.



I went back to the original code and I logged in as Eniola Olaogun and the email was echoed, but as soon as I changed the person I logged in as, no email was echoed.



I am not sure why I am experiencing this problem and some help would be greatly appreciated










share|improve this question




















  • 1





    mysqli_error($con) that won't work, you're using a different variable.

    – Funk Forty Niner
    Nov 24 '18 at 17:30














-1












-1








-1








I am making a profile page in which I want the user's email to be displayed. I thought this would be quite a simple code that could be achieved using the select function from the database. However, this only works for one string and I cannot seem to figure out why.



This is my original code



session_start();

$_SESSION["user"] = $username;
$_SESSION["pass"] = $password;
$_SESSION["email"] = $email;
$connection = mysqli_connect ("localhost", "root", "", "picshare");

if ($connection ->connect_error) {
die("Connection failed: " . $connection->connect_error);
}else{

$query = mysqli_query($connection, "SELECT email FROM login WHERE username='".$_SESSION["user"]."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);


}else{
echo "error";
$conn->close();

}}


When I try and echo $field, nothing was echoed



            <p class ="right uc"><?php       echo($field['email']);?></p> 


I retried the code, but instead of using a session, I made a variable



$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "picshare";
$user = 'Eniola Olaogun';

$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{

$query = mysqli_query($conn, "SELECT email FROM login WHERE username='".$user."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);
echo($field['email']);

}else{
echo "error";
$conn->close();


This code displayed the email, and so I proceeded to change the $user variable to another name and the original problem occurred where nothing was echoed.



I went back to the original code and I logged in as Eniola Olaogun and the email was echoed, but as soon as I changed the person I logged in as, no email was echoed.



I am not sure why I am experiencing this problem and some help would be greatly appreciated










share|improve this question
















I am making a profile page in which I want the user's email to be displayed. I thought this would be quite a simple code that could be achieved using the select function from the database. However, this only works for one string and I cannot seem to figure out why.



This is my original code



session_start();

$_SESSION["user"] = $username;
$_SESSION["pass"] = $password;
$_SESSION["email"] = $email;
$connection = mysqli_connect ("localhost", "root", "", "picshare");

if ($connection ->connect_error) {
die("Connection failed: " . $connection->connect_error);
}else{

$query = mysqli_query($connection, "SELECT email FROM login WHERE username='".$_SESSION["user"]."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);


}else{
echo "error";
$conn->close();

}}


When I try and echo $field, nothing was echoed



            <p class ="right uc"><?php       echo($field['email']);?></p> 


I retried the code, but instead of using a session, I made a variable



$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "picshare";
$user = 'Eniola Olaogun';

$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{

$query = mysqli_query($conn, "SELECT email FROM login WHERE username='".$user."'");
$field = mysqli_fetch_assoc($query);

if (!$query)
{
die('Error: ' . mysqli_error($con));
}if(mysqli_num_rows($query) > 0){
$field = mysqli_fetch_assoc($query);
echo($field['email']);

}else{
echo "error";
$conn->close();


This code displayed the email, and so I proceeded to change the $user variable to another name and the original problem occurred where nothing was echoed.



I went back to the original code and I logged in as Eniola Olaogun and the email was echoed, but as soon as I changed the person I logged in as, no email was echoed.



I am not sure why I am experiencing this problem and some help would be greatly appreciated







php html mysql database session






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:30









Funk Forty Niner

1




1










asked Nov 24 '18 at 16:20









E. OlaogunE. Olaogun

1




1








  • 1





    mysqli_error($con) that won't work, you're using a different variable.

    – Funk Forty Niner
    Nov 24 '18 at 17:30














  • 1





    mysqli_error($con) that won't work, you're using a different variable.

    – Funk Forty Niner
    Nov 24 '18 at 17:30








1




1





mysqli_error($con) that won't work, you're using a different variable.

– Funk Forty Niner
Nov 24 '18 at 17:30





mysqli_error($con) that won't work, you're using a different variable.

– Funk Forty Niner
Nov 24 '18 at 17:30












2 Answers
2






active

oldest

votes


















0














$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "picshare";
$user = 'Eniola Olaogun';

$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
$sql = "SELECT email FROM login WHERE username= {$user}";
$result = $conn->query($sql);

if($result->num_rows > 0) {
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
echo($row['email']);
}
else {
echo "error";
$conn->close();





share|improve this answer































    -1














    error connecting to mysql database through crazy domains



    It sounds like it is a permission issue based on the user you're logging in as.



    Test it with a 'root' user password that has global access and then troubleshoot and isolate it from there. I'm betting you will find it then.



    Pretty much impossible for me to test this remotely since I don't have your DB schema and user accounts to validate with.






    share|improve this answer
























    • check code there is root user for connection already.

      – Akhilesh
      Nov 24 '18 at 17:21











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460075%2fselect-data-from-a-row-in-database%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    $host = "localhost";
    $dbusername = "root";
    $dbpassword = "";
    $dbname = "picshare";
    $user = 'Eniola Olaogun';

    $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    else {
    $sql = "SELECT email FROM login WHERE username= {$user}";
    $result = $conn->query($sql);

    if($result->num_rows > 0) {
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
    echo($row['email']);
    }
    else {
    echo "error";
    $conn->close();





    share|improve this answer




























      0














      $host = "localhost";
      $dbusername = "root";
      $dbpassword = "";
      $dbname = "picshare";
      $user = 'Eniola Olaogun';

      $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

      if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
      }
      else {
      $sql = "SELECT email FROM login WHERE username= {$user}";
      $result = $conn->query($sql);

      if($result->num_rows > 0) {
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      echo($row['email']);
      }
      else {
      echo "error";
      $conn->close();





      share|improve this answer


























        0












        0








        0







        $host = "localhost";
        $dbusername = "root";
        $dbpassword = "";
        $dbname = "picshare";
        $user = 'Eniola Olaogun';

        $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        }
        else {
        $sql = "SELECT email FROM login WHERE username= {$user}";
        $result = $conn->query($sql);

        if($result->num_rows > 0) {
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
        echo($row['email']);
        }
        else {
        echo "error";
        $conn->close();





        share|improve this answer













        $host = "localhost";
        $dbusername = "root";
        $dbpassword = "";
        $dbname = "picshare";
        $user = 'Eniola Olaogun';

        $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);

        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        }
        else {
        $sql = "SELECT email FROM login WHERE username= {$user}";
        $result = $conn->query($sql);

        if($result->num_rows > 0) {
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
        echo($row['email']);
        }
        else {
        echo "error";
        $conn->close();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 4:00









        Mohsin MujawarMohsin Mujawar

        666




        666

























            -1














            error connecting to mysql database through crazy domains



            It sounds like it is a permission issue based on the user you're logging in as.



            Test it with a 'root' user password that has global access and then troubleshoot and isolate it from there. I'm betting you will find it then.



            Pretty much impossible for me to test this remotely since I don't have your DB schema and user accounts to validate with.






            share|improve this answer
























            • check code there is root user for connection already.

              – Akhilesh
              Nov 24 '18 at 17:21
















            -1














            error connecting to mysql database through crazy domains



            It sounds like it is a permission issue based on the user you're logging in as.



            Test it with a 'root' user password that has global access and then troubleshoot and isolate it from there. I'm betting you will find it then.



            Pretty much impossible for me to test this remotely since I don't have your DB schema and user accounts to validate with.






            share|improve this answer
























            • check code there is root user for connection already.

              – Akhilesh
              Nov 24 '18 at 17:21














            -1












            -1








            -1







            error connecting to mysql database through crazy domains



            It sounds like it is a permission issue based on the user you're logging in as.



            Test it with a 'root' user password that has global access and then troubleshoot and isolate it from there. I'm betting you will find it then.



            Pretty much impossible for me to test this remotely since I don't have your DB schema and user accounts to validate with.






            share|improve this answer













            error connecting to mysql database through crazy domains



            It sounds like it is a permission issue based on the user you're logging in as.



            Test it with a 'root' user password that has global access and then troubleshoot and isolate it from there. I'm betting you will find it then.



            Pretty much impossible for me to test this remotely since I don't have your DB schema and user accounts to validate with.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 16:33









            WaxhawWaxhaw

            274




            274













            • check code there is root user for connection already.

              – Akhilesh
              Nov 24 '18 at 17:21



















            • check code there is root user for connection already.

              – Akhilesh
              Nov 24 '18 at 17:21

















            check code there is root user for connection already.

            – Akhilesh
            Nov 24 '18 at 17:21





            check code there is root user for connection already.

            – Akhilesh
            Nov 24 '18 at 17:21


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460075%2fselect-data-from-a-row-in-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen