-1

I have a php/mysql site which also connects to an mssql database to run some stored procedures. There is a form that asks for postcode, distance from postcode and returns effectively the stores within that distance of the postcode. For some reason when this function is run and there are no results the form returns correctly with a message saying no results found. If there are results for some reason the browser soft redirects back to the homepage. If I comment out the mssql_execute part of the function the redirect doesnt happen, obviously I get no results. When I put the mssql_execute in and run the same request the page is redirected. The function with the stored procedure execution is below.

function getCompaniesByAddress(){
    global $msdb;

    //initiate function
$proc = mssql_init('usp_Search_Scope_And_Range', $msdb); 

//Load Parameters 
mssql_bind($proc, '@SuperscopeID', $_POST['activity'], SQLINT4, false, false, 10);
mssql_bind($proc, '@ScopeID', $_POST['scheme'], SQLINT4, false, false, 10);
mssql_bind($proc, '@PCode', $_POST['postcode'], SQLVARCHAR, false, false, 10);
mssql_bind($proc, '@Distance', $_POST['distance'], SQLINT4, false, false, 10);

try
  {
  $result = mssql_execute($proc);
  //If the exception is thrown, this text will not be shown
  }

//catch exception
catch(Exception $e)
  {
  echo 'Message: ' .$e->getMessage();
  }

//Execute Procedure 
//$result = mssql_execute($proc);

//Free Memory 
mssql_free_statement($proc); 

while ($row = mssql_fetch_assoc($result))
    $results[] = $row;

$res = array_chunk(sortDataSet($results,'Mileage_FL'),20);

return $res[0];
}
mattwadey
  • 51
  • 4
  • And did you search your code for `set_exception_handler`? – Aaron Bertrand Apr 25 '12 at 17:44
  • 1
    Do you have access to the SQL Server so that you can run the SP on the server? – swasheck Apr 25 '12 at 17:53
  • Also, are you *sure* you're connected? If so, are you *sure* the command is completing? Is there any javascript (with potential logic errors) in there that may be mistakenly redirecting when the result set is received? Is there any way you can code up a straight PHP page to call this function and print the result (without any HTML modification/beautification ... just straight data dump)? Better yet, can you quickly write up a PHP page that prints *all* results? – swasheck Apr 25 '12 at 17:58
  • 2
    ...and run any mock-ups outside of your primary web application, as I don't know exactly how the global error handling is being enforced. – Aaron Bertrand Apr 25 '12 at 18:02
  • ive propagated a diff domain to put a demo version up so you can have a look. thenewcastlepokerforum.co.uk/security-provider-search doing a postcode based search will run the function you can find a working copy of exactly the same code at ssaib.armadillo-demo.co.uk/security-provider-search – mattwadey Apr 25 '12 at 20:13
  • soooooooo ... does that mean you've implicitly answered the questions in my comment? – swasheck Apr 25 '12 at 20:15
  • it means like i've mentioned that the code is fine barre the chance the code is conflicting with the server settings in some way. Exactly the same code works fine on ssaib.armadillo-demo.co.uk but does the crazy redirect on thenewcastlepokerforum.co.uk – mattwadey Apr 25 '12 at 20:18
  • There are just so many variables. – swasheck Apr 25 '12 at 20:29
  • not many that directly impact the function bearing in mind it works perfectly on one server. the anomaly is the server but working out what to do to fix it is the hard part – mattwadey Apr 25 '12 at 20:30

1 Answers1

2

Can't you employ try/catch here? Then you can at least print out the error message that's being returned (you're obviously not showing any global error handling that is just throwing you into the redirect). I'm not a PHP guy so I don't know if this syntax is 100% accurate, but you could try something like:

try
{
  $result = mssql_execute($proc);
  while ($row ...
  ...
  return $res[0];
}
catch (Exception $e) 
{
  echo ($e->getMessage());
}

(Based on the example here: try catch statement in PHP where the file does not upload)

Community
  • 1
  • 1
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • Yeah I did try that but the redirect fires as soon as the mssql_execute runs so by the time you would hit if(!$result){ It already would have fired the redirect. – mattwadey Apr 25 '12 at 17:24
  • The redirect happens even if you wrap it with `try`? That sounds wrong to me. Can you show that code in your question? I suspect there is some error handling here that is not reflected in your question. The whole point of try/catch is to be able to handle exceptions. If some other part of the code is taking over... – Aaron Bertrand Apr 25 '12 at 17:26
  • Yeah i Tried it, it still just fires the redirect. To be honest I'm sure it's an issue with the server not the syntax as it works for me on a different server. – mattwadey Apr 25 '12 at 17:30
  • I would grep through all the files in your site and see if you have `set_exception_handler(` somewhere. Chances are you are overriding exception handling and the handler function just performs a redirect. You can probably edit that function to print out the exception instead (or just comment out the redirect *temporarily*, in case it is somehow overriding your local `try` handling). – Aaron Bertrand Apr 25 '12 at 17:38
  • Ive just run it all again and manually written a return array so I could comment out the mssql_execute while still allowing the rest of th script to run as expected. The page didnt redirect with mssql_execute commented out but did with it in the script. So it is definitely that 1 line causing a redirect. The term "set_exception_handler" doesnt appear anywhere in whole site syntax – mattwadey Apr 25 '12 at 17:39
  • Yes, I think you've already proven multiple times that the stored procedure call is the culprit. Can you show the version of the script that has `try` / `catch` implemented? The redirect should only happen for *uncaught* exceptions (e.g. those without `try`, or those with only `try` and no `catch`). Please add this to the question rather than trying to post it in a comment. – Aaron Bertrand Apr 25 '12 at 17:40