开发者

Query doesn't work inside a function

开发者 https://www.devze.com 2023-03-18 22:20 出处:网络
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 AND id_group开发者_开发知识库 != 12 OR

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 AND id_group开发者_开发知识库 != 12 ORDER BY id_group ASC";

$result = mysql_query($query, $con);


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

while ($row = mysql_fetch_array($result)) {

Display($row['id_member']);

$resultDisplay = mysql_query($queryDisplay, $con);

echo ("<td>"); 

if (mysql_num_rows($resultDisplay) == 0) echo ("Not yet entered");
else {
 while ($rowDisplay = mysql_fetch_array($resultDisplay)) {
  if (strlen($rowDisplay['value']) > 0) echo ("".$rowDisplay['value'].""); 
 }
}
echo ("</td>"); 

}

If I echo ($member_id); it works just fine, but as soon as I put it like there ^, it doesn't do anything.

What am I doing wrong? :(


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.


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.


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号