0

I have created this select function with no escape to run simple queries where the data isn't coming from a user.

My function works and I can do what I intended with global $var however when I tried to return the value rather than make it a global variable it didn't work. I am just curious as to why this is.

Here is my function:

    function SelectQuery_NO_ESCAPE($row, $table, $row, $value) {
    $Database = DatabaseConnection();
    $sql_query = "SELECT $row FROM $table WHERE $row = '$value'";
    $select_result = $Database['Connection']->query($sql_query) or die(mysqli_error($Database['Connection']));
    if ($select_row = $select_result->fetch_assoc()) {
        global $select_row;
        $select_row = $select_row[$row];

    }
}

I have a test.php file where I use the function:

SelectQuery_NO_ESCAPE('ip', 'ip_address', 'ip', '1');
echo $select_row;

This works and outputs 1. If I try to take out the global and just return the variable $row_value it doesn't work.

Why is this?

4
  • 4
    where is the return in the function? From what I see, it doesn't return anything. Commented Jul 21, 2014 at 19:23
  • 4
    Do you really have two different $row arguments for your function? Commented Jul 21, 2014 at 19:24
  • 2
    Because you probably are doing nothing with the return value of the function. echo SelectQuery... would work. Commented Jul 21, 2014 at 19:24
  • 1
    @MarkBaker Oops my bad, I typed it out wrong, that wasn't meant to be like that. Commented Jul 21, 2014 at 19:25

1 Answer 1

4

Return it:

if ($select_row = $select_result->fetch_assoc()) {
    return $select_row[$row];
}

Assign the returned value:

$something = SelectQuery_NO_ESCAPE('ip', 'ip_address', 'ip', '1');
echo $something;
//or
echo SelectQuery_NO_ESCAPE('ip', 'ip_address', 'ip', '1');
Sign up to request clarification or add additional context in comments.

1 Comment

That's why, thank you. I will accept in 10 minutes when it allows me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.