SQL query result in a string (or variable)

25,365

Solution 1

I'm assuming you are doing something like:

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

The 'i' in mysqli is for 'improved' which is what you should be using these days.

This $result is not your output yet. You must call:

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

The above code gives you $row, which is your actual output, in the form of an array.

Solution 2

$result = mysql_query($sql);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("Name: %s", $row['agent_fname'],);  
}
Share:
25,365
Admin
Author by

Admin

Updated on August 05, 2020

Comments

  • Admin
    Admin over 3 years

    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.

    Using this query:

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

    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?

    Thank you!

    *I have more than one row.