1

I am trying to create a function in PHP that accepts two arguments, one being the SQL for a mysqli query and the other being the name I would like to be set as the variable name for the resulting array. Here it is so far:

<?php

function dbSelect($sql,$name) {
    include "connect.php";
    if($results = $db->query($sql)) {
        if($results->num_rows) {
            while($row = $results->fetch_object()) {
                ${$name}[] = $row;
            }
            $results->free();
        }
    }
    return ${$name};
}

Within the referenced connect.php file is this code

$db = new mysqli('127.0.0.1', 'admin', 'PASSWORD', 'DB_NAME');

However it does not seem to work in its current state. I do not get any errors when calling the function "dbSelect($sql, 'test');" however when I try to reference the supposedly created variable (in this case, $test) I get an undefined error.

Any suggestions or tips on how to fix this?

Thanks in advance.

3
  • Seems like you're using, what seems to be a variables variable method. Have you tried $name[] = $row; ... return $name;? Commented Jun 9, 2014 at 18:09
  • If I do that I get an error Fatal error: [] operator not supported for strings Commented Jun 9, 2014 at 18:12
  • If there are no results returned from query, your array is not inizialized so you try to return a null array, you should initzialize $name with $name = array(); Commented Jun 9, 2014 at 18:12

2 Answers 2

2

The ${$name} variable you're assigning to has local scope. That is, the variable disappears as this function returns. Using a variable-variable doesn't make that variable global.

I would recommend you just name the array anything inside your function, and then return the array. Then you can assign its return value to your desired variable instead of passing the variable name.

$test = dbSelect($sql);

You could try to declare global ${$name} inside the function to write to a variable of global scope, but IMHO it's not worth it. It's a better habit to avoid writing functions that have weird side effects on global variables. Keep it simple, just return the results.

Sign up to request clarification or add additional context in comments.

6 Comments

Downvoter, can you explain why you think this is wrong? Perhaps I can improve it.
The OP's design is dubious at best, but misunderstanding scope is likely the issue causing his problems here, so +1 from me :)
"Downvoter, can you explain why you think this is wrong? Perhaps I can improve it." - I couldn't agree with you more Bill. Downvoters hardly ever post a comment, only the ones with balls and the ones who don't hide behind an LCD curtain.
Im just trying to create some functionality while still using PHP, any tips on the methods I use are welcome as well :)
@Fred-ii-, I see from the edit above that this question has the attention of YourCommonSense, which explains the unwarranted downvote.
|
2

The variable variable ${$name} has no practical use in this function. The function returns an array with no variable name. You name it when you assign the function return:

$whatEverYouWant = dbSelect('some sql');

function dbSelect($sql) {
    include "connect.php";
    if($results = $db->query($sql)) {
        if($results->num_rows) {
            while($row = $results->fetch_object()) {
                $array[] = $row;
            }
            $results->free();
            return $array;
        }
    }
    return false;    
}

Return false or maybe an empty array() if there are no results or an error.

1 Comment

Which resembles my comment about $name[] = $row; ... return $name; yet the OP stated "error Fatal error: [] operator not supported for strings" - It's just the names are different.

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.