[英]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. 我有一个PHP文件,它将运行SQL select语句并回显结果。 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? IE:根据下面的代码,如何创建一个名为$ FirstName的变量并将其设置为等于返回的firstname列中单个记录的值?
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. 我将其作为注释,但如果有人与@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. 但您必须知道在尝试将
$row
分配给变量(在这种情况下,在循环内)之前,您可以访问该$row
(或以正确的方式访问该内容)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.