这是整个函数吗?您甚至没有运行查询。您所做的只是用 sql 创建一个字符串而不是运行它。
Edit:
我认为您需要帮助来理解 PHP 中的变量范围。
http://php.net/manual/en/language.variables.scope.php
基本上,它表示函数内部的代码无法访问函数外部定义的变量(大多数变量)。例如:
$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();
这也适用于函数内部的变量无法在该函数外部访问的情况。这就是你做错的事情。
function testFunc(){
$someNewVar = "some new string";
}
testFunc();
//$someNewVar is never defined outside the function so it doesn't exist.
echo $someNewVar;
一旦函数运行完毕,在函数内部声明和使用的所有变量都会从内存中删除。
因此,要将变量放入函数中,您需要将其作为参数传递。要获取变量,您需要函数返回该变量。
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 页面会比我更好地描述它。但这应该能让您了解问题所在。