SQL query results in a string (or variable)

Is it possible to output the result of an SQL query in a single line 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.

Using this query:

$sql = SELECT a.`agent_fname` FROM agents a WHERE a.`agent_dept` = 'FCA'

I want to get a string or variable, so I can use it anywhere in my php file (i.e. $ my_variable, which prints the result of my query). Is it possible? If so, how?

Thanks!

* I have a few lines.

+5
source share
3 answers

I assume you are doing something like:

$result = mysqli_query ($link, $sql).

"i" mysqli - "", .

$result . :

while ($row = mysqli_fetch_assoc($result)) {
     print_r ($row);
}

$row, , .

+5
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("Name: %s", $row['agent_fname'],);  
}
+3

, . . :

$records = array();
$mysqli_result = mysqli_query ($link, $sql);

while ($row = mysqli_fetch_assoc($result)) {
     $records[] = $row;
}

, $records, . . (, .) , , .

$mysqli_result.

0

All Articles