I am wondering I have been trying to place me MySQL query and MySQL result code into a PHP function like this
我一直想把MySQL查询和MySQL结果代码放到PHP函数中。
function setting($claim){
$query = "SELECT `cases`, `hg`, `surname`, `firstname`, `type`, `claim`, `charge`, `damage`, `payment`, `repair`, `returned`, `comments`, `cost` FROM `rep_log` WHERE claim='$claim'";
$result = mysql_query($query) or die(mysql_error());
}
I'm trying to use this so that I can make it easier to change what is being selected without having to have heaps of different variables and stuff just to change the query... so basically what I do is
我正在尝试使用它,这样我就可以更容易地改变被选中的东西,而不必有很多不同的变量和东西来改变查询。基本上我所做的就是。
echo setting("warrenty");
But I'm getting an error:
但是我有一个错误
Warning: mysql_fetch_row() expects parameter 1 to be resource, string given in ...
警告:mysql_fetch_row()希望参数1是资源,字符串在…
So I am wondering is it even possible to put a MySQL query and result into a function or is it just something which cannot happen...
我想知道是否可以将一个MySQL查询结果放入一个函数中或者它只是不能发生的事情…
If it is possible any help would be great.
如果有可能,任何帮助都是巨大的。
COMPLETE CODE
完整的代码
<?
// connection with the database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "netbookdb";
$result="";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
// require the PHPExcel file
require 'Classes/PHPExcel.php';
// simple query
function setting($claim){
$query = "SELECT `cases`, `hg`, `surname`, `firstname`, `type`, `claim`, `charge`, `damage`, `payment`, `repair`, `returned`, `comments`, `cost` FROM `rep_log` WHERE claim='$claim'";
$result = mysql_query($query) or die(mysql_error());
}
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('Insurance');
echo setting('Warrenty');
$rowNumber = 1;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="myFile.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
echo 'a problem has occurred... no data retrieved from the database';
?>
2 个解决方案
#1
4
You are not returning any result out of a function.
你不会从函数中返回任何结果。
Use return $result;
使用美元返回结果;
UPDATE 1:
更新1:
You have not decalred the $result variable
which you are trying to use in mysql_fetch_row
so first return the value from the function assign it and then use it.
您没有将试图在mysql_fetch_row中使用的$result变量进行decalred,因此首先从函数中返回值,然后使用它。
UPDATE 2:
更新2:
function setting($claim){
$query = "SELECT `cases`, `hg`, `surname`, `firstname`, `type`, `claim`, `charge`, `damage`, `payment`, `repair`, `returned`, `comments`, `cost` FROM `rep_log` WHERE claim='$claim'";
$result = mysql_query($query) or die(mysql_error());
return $result;
}
and then get the output of function in a variable which you are using in mysql_fetch_row
method. i.e.
然后在mysql_fetch_row方法中使用的变量中得到函数的输出。即。
$result = setting('Warrenty');
Hope this helps.
希望这个有帮助。
#2
0
You are assigning a local "$result" variable, so "mysql_fetch_row($result)" is using the string variable "$result", initialized as "$result = ''", that is not a resource and therefore will raise the exception.
您正在分配一个本地的“$result”变量,因此“mysql_fetch_row($result)”使用字符串变量“$result”,初始化为“$result =”,这不是一个资源,因此将引发异常。
In the "setting" function, put add "global $result;" line:
在“设置”功能中,添加“全局$result”行:
function setting($claim){
global $result;
$query = "...;
...
}
#1
4
You are not returning any result out of a function.
你不会从函数中返回任何结果。
Use return $result;
使用美元返回结果;
UPDATE 1:
更新1:
You have not decalred the $result variable
which you are trying to use in mysql_fetch_row
so first return the value from the function assign it and then use it.
您没有将试图在mysql_fetch_row中使用的$result变量进行decalred,因此首先从函数中返回值,然后使用它。
UPDATE 2:
更新2:
function setting($claim){
$query = "SELECT `cases`, `hg`, `surname`, `firstname`, `type`, `claim`, `charge`, `damage`, `payment`, `repair`, `returned`, `comments`, `cost` FROM `rep_log` WHERE claim='$claim'";
$result = mysql_query($query) or die(mysql_error());
return $result;
}
and then get the output of function in a variable which you are using in mysql_fetch_row
method. i.e.
然后在mysql_fetch_row方法中使用的变量中得到函数的输出。即。
$result = setting('Warrenty');
Hope this helps.
希望这个有帮助。
#2
0
You are assigning a local "$result" variable, so "mysql_fetch_row($result)" is using the string variable "$result", initialized as "$result = ''", that is not a resource and therefore will raise the exception.
您正在分配一个本地的“$result”变量,因此“mysql_fetch_row($result)”使用字符串变量“$result”,初始化为“$result =”,这不是一个资源,因此将引发异常。
In the "setting" function, put add "global $result;" line:
在“设置”功能中,添加“全局$result”行:
function setting($claim){
global $result;
$query = "...;
...
}