0

I'm working on a project which requires more than one column contents to be passed to a php variable

I am able to select and pass one column content to the variable but failed on multiple columns

$myEMPNEM = "";

$sqlNEM = "SELECT first_name, middle_name, last_name, job_title FROM 
t_employees WHERE user_name = '" . $_SESSION["uname"] . "'";
$resultNEM = mysqli_query( $conn, $sqlNEM );
while($row = mysqli_fetch_array($resultNEM))
{
  $myEMPNEM = $row['first_name'];   
 }

I expect more than one column content to be passed to the php variable

8
  • 1
    To which PHP variable? Commented Jul 29, 2019 at 16:24
  • You are only assigning first_name. Why would you expect more? Commented Jul 29, 2019 at 16:26
  • The $myEMPNEM variable Commented Jul 29, 2019 at 16:26
  • 1
    Don't assume $_SESSION array to be safe, because it might not be the case on shared webhosting (with bad configurations) .. General rule when working with a database always always always always always always always always use prepared statements no exceptions Commented Jul 29, 2019 at 16:47
  • 1
    Thank you @RaymondNijland i will work on it Commented Jul 29, 2019 at 16:52

1 Answer 1

1

You either need to use the variable as an array:

while($row = mysqli_fetch_array($resultNEM))
{
  $myEMPNEM = array($row['first_name'], $row['middle_name'], $row['last_name']);   
}

Or concatenate the values together into a single string:

while($row = mysqli_fetch_array($resultNEM))
{
  $myEMPNEM = $row['first_name'] . " " . $row['middle_name'] . " " . $row['last_name'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Concatenating the values together into a string works! I appreciate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.