简体   繁体   中英

Create PHP Variables for all columns in SQL query

I have a PHP file that will run a SQL select statement and echo out the results. I am trying to be able to set individual columns to specific variables. Is this possible?

IE: based on the code below, how could I create a variable called $FirstName and set it equal to the value of the single record in the column firstname that is returned?

Thank you

   $sql = "SELECT * FROM Employees where Employee_ID = 1";
  $stmt = sqlsrv_query( $trpConn, $sql );
  if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
  }

  while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    echo $row['FirstName'].", ".$row['LastName']."<br />";
  } 

I put it as a comment but I provide it as an answer to help if someone has the same error as @AAA. If you want to store your value you just have to put it as follows:

$FirstName = $row['FirstName'];

but you have to aware that you have access to that $row (or that you are accessing in the right way to that content) before trying to assign it to a variable, in this case, inside the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM