1

I am wondering I have been trying to place me MySQL query and MySQL result code into a PHP function like this

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

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

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';
?>
4
  • Did you know that have spelled warranty incorrectly? Commented May 25, 2012 at 5:35
  • im getting an error that basicly says that no query has been ran and no result has been returned... Can you please post the exact error message you get instead of writing it in your own words? Commented May 25, 2012 at 5:37
  • this error <b>Warning</b>: mysql_fetch_row() expects parameter 1 to be resource, string given in Commented May 25, 2012 at 5:39
  • 1
    Where do you call mysql_fetch_row()? Can you post the code for that? Commented May 25, 2012 at 5:40

2 Answers 2

4

You are not returning any result out of a function.

Use return $result;

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

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

$result = setting('Warrenty');

Hope this helps.

Sign up to request clarification or add additional context in comments.

6 Comments

i am trying to use the result later on in the script but that all works fine its only when its in the fucntion does it not work
@Matthew: Have you tried doing what AlphaMale has suggested? If not, please try it first before commenting on why you think it might not work. If you have tried it then be more specific about what new problem you have. Post the new error message.
@MarkByers you are right he is not using the result variable correctly.
i have read the update but i dont understand what u mean one i echo the function shouldn't MySQL_fetch_row see the result and continue to run
Nope. unless and untill you return the result.
|
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.

In the "setting" function, put add "global $result;" line:

function setting($claim){
global $result;
    $query = "...;
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.