查询在函数内部不起作用 [英] Query doesn't work inside a function

查看:106
本文介绍了查询在函数内部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在SMF的自定义脚本中具有此功能:

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>"); 

}

如果我回声($ member_id);它工作正常,但是只要我把它放在那里^,它就什么也不会做.

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

我做错了什么? :(

推荐答案

是整个功能吗?您甚至都没有运行查询.您要做的就是用sql创建一个字符串,然后不运行它.

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.

我认为您需要帮助来了解PHP中的变量范围.

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".

老实说,php页面将比我能更好地描述它.但这应该可以让您了解出什么问题了.

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

这篇关于查询在函数内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆