Need Help With Custom SQL Query

Need Help With Custom SQL Query

by Leigh Dragoon -
Number of replies: 3

I'm really at my wit's end here.  Basically, the situation is this: I am currently using a feedback form to gather a very important piece of info from our course users. I have written a custom SQL query which I use in a report page to display the user's data from this one feedback form table field.  As it stands, with the sql query in the report page, it works perfectly and I can pull the data from the correct table field, no problem.  Great, right?

Well, the problem is that I will need several of these queries in the future, and I'd rather they were in their own tidy little lib file as opposed to all grouped in the report page.  So I want to create a function that sits in the lib file to run the SQL query and output the result, and then call that function from the report page.

I cannot figure this out to save my life. I will admit, I am a total beginner level PHP coder. I feel like I'm missing some completely simple, obvious problem. I have included my code below:

This is the query which sits on the report page and works perfectly:

$sql = "SELECT
fc . *,
fk . *,
fv.item,
fv.value,
u . *
FROM
{$CFG->prefix}feedback_completed fc,
{$CFG->prefix}feedback fk,
{$CFG->prefix}feedback_value fv,
{$CFG->prefix}user u
WHERE
u.id = {print_object($user->id)} AND fv.completed = fc.id AND fc.userid = {print_object($user->id)} AND fv.item = 3";

$dprnumber = get_record_sql($sql);

I call it a little further down the page and print it into a table:

$licenseNo = $dprnumber->value;

Again, this works, perfectly, every time.

So here's what I'm trying to put into the lib file:

function test_function($userid){   
global $CFG;

$sql = "SELECT
fc . *,
fk . *,
fv.item,
fv.value,
u . *
FROM
{$CFG->prefix}feedback_completed fc,
{$CFG->prefix}feedback fk,
{$CFG->prefix}feedback_value fv,
{$CFG->prefix}user u
WHERE
u.id = '$userid' AND fv.completed = fc.id AND fc.userid = '$userid' AND fv.item = 3";

$output = get_record_sql($sql);

return $output;
}

I try to call the function with this, back in the report file:

echo '<div style="border: 1px solid blue; padding: 5px;">';
test_function($user->id);
echo '</div>';

I have tried everything I can think of and I can't get it to work.  I've passed a text value to make sure the function call on the report page is actually "finding" the function in the lib file, and it prints that text value correctly, which leads me to think that the problem is most likely some silly mistake I've made with the SQL query, or values I'm not passing through the function properly.

I would greatly appreciate a little help.  There's pretty much just me here working on this so if I don't figure it out my department is in a pickle. sad

We are using version 1.9.5.

Average of ratings: -
In reply to Leigh Dragoon

Re: Need Help With Custom SQL Query

by Ann Adamcik -

You don't seem to be doing anything with the return value from your function - e.g.

if ($result = test_function($user->id)) { echo $result; }

In reply to Ann Adamcik

Re: Need Help With Custom SQL Query

by Leigh Dragoon -

Thank you very much, Ann -- I'll see if this does the trick!

In reply to Ann Adamcik

Re: Need Help With Custom SQL Query

by Leigh Dragoon -

Thank you, Ann - part of the problem was what you noted, and part was that I did not have the SQL query set up correctly.  I fixed the query and now it's working. smile