SQL 查询结果为字符串(或变量) [英] SQL query result in a string (or variable)
问题描述
是否可以将 SQL 查询结果输出为一个字符串或变量?(我在 php 和 mysql 方面很差)假设我有包含列的数据库代理" - agent_id、agent_fname、agent_lname、agent_dept.
Is it possible to output SQL query result in one string or variable? (i'm bad in php and mysql) Let's say I have db "agents" with columns - agent_id, agent_fname, agent_lname, agent_dept.
使用此查询:
$sql = SELECT a.`agent_fname` FROM agents a WHERE a.`agent_dept` = 'FCA'
我想获得一个字符串或一个变量,所以我可以在我的 php 文件的任何地方使用它(即输出我的查询结果的 $my_variable).是否可以?如果是 - 如何?
I want to get a string or a variable, so I can use it in any place of my php file (i.e. $my_variable which outputs the result of my query). Is it possible? If yes - how?
谢谢!
*我有不止一行.
推荐答案
我假设您正在执行以下操作:
I'm assuming you are doing something like:
$result = mysqli_query ($link, $sql).
mysqli 中的i"用于改进",这是您现在应该使用的.
The 'i' in mysqli is for 'improved' which is what you should be using these days.
这个 $result 还不是你的输出.您必须致电:
This $result is not your output yet. You must call:
while ($row = mysqli_fetch_assoc($result)) {
print_r ($row);
}
上面的代码以数组的形式为您提供了 $row,这是您的实际输出.
The above code gives you $row, which is your actual output, in the form of an array.
这篇关于SQL 查询结果为字符串(或变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!