Query doesn't work inside a function

前端 未结 5 922
一生所求
一生所求 2021-01-25 09:27

Well, I have this function in a custom script for SMF:

$query = \"SELECT id_member, real_name, id_group FROM smf_members WHERE id_group > 0 AND id_group != 9          


        
相关标签:
5条回答
  • 2021-01-25 09:55

    You are just preparing sql query, but not executing it.

    0 讨论(0)
  • 2021-01-25 09:57

    your query

    function Display($member_id){
    $queryDisplay = "SELECT value FROM smf_themes WHERE id_member = ".$member_id." AND variable = 'cust_realfi'";
    }
    

    right query

        function Display($member_id){
        $queryDisplay = mysql_query("SELECT value FROM smf_themes WHERE id_member = '$member_id' AND variable = 'cust_realfi'");
        while ($row = mysql_fetch_array($queryDisplay));
        return $row['value'];
        }
    
    0 讨论(0)
  • 2021-01-25 10:02

    Your function doesn't do anything: It assigns a value to a local variable, and then does nothing with the value and the variable. It should either return it (the value) or execute the query.

    Also note that doing string concatenation to put the variable in the query is creating a security hole: http://bobby-tables.com/

    You should consider using mysqli and parametrized queries as suggested here: http://bobby-tables.com/php.html

    At the very least, consider quoting the values you include in the query with the functions provided by PHP to do so. All values. But parametrized queries are better and easier.

    0 讨论(0)
  • 2021-01-25 10:06

    is that the entire function? You are not even running the query. All you are doing is making a string with the sql and not running it.

    Edit:

    I think you need help in understanding variable scope in PHP.

    http://php.net/manual/en/language.variables.scope.php

    Basically it says that the code inside of a function has no access to variables (most variables) defined outside of the function. So for example:

    $test = "test value";
    
    function testFunc(){
        //Since the code inside this function can't
        //access the variables outside of this function
        //the variable $test below is just an empty
        //variable.
        echo $test;
    }
    testFunc();
    

    This also works in reverse where variables inside of a function can't be accessed outside that function. This is what you are doing wrong.

    function testFunc(){
        $someNewVar = "some new string";
    }
    testFunc();
    
    //$someNewVar is never defined outside the function so it doesn't exist.
    echo $someNewVar;
    

    Once a function is done running, all variables declared and used locally inside of it are deleted from memory.

    So to get a variable into the function, you need to pass it as an argument. To get a variable out, you need for the function to return the variable.

    function testFunc($testVar){
        echo $testVar;
        $testVar = "some new string";
        return $testVar;
    }
    $test = "test val";
    //passing the variable into the function and setting
    //the return value back into $test.
    $test = testFunc($test);
    echo $test; //test now has "some new string".
    

    Honestly, the php page will describe it better than I can. but this should give you an idea of what is wrong.

    0 讨论(0)
  • 2021-01-25 10:09

    you function only makes a variable $queryDisplay (that is traped within the scope of the function)

    0 讨论(0)
提交回复
热议问题