0

I have 1,5k lines od PHP spaghetti code that have dozens of if's generating dozens more queries.

All the conditions are simple "==" or "!=" against list of ~20 possibilities.

Now I need to add 21st... I need to:

  • make sure that no unwanted query is made
  • see what queries are made for other possibilities to make sure that I implement all necessary queries

How can I enable query logging ( ext of executed query will be enough ) for just that single .php file if code use ODBC php module to talk to SQL Server?

przemo_li
  • 3,932
  • 4
  • 35
  • 60
  • 2
    Isn't it better to do some refactoring? 1.5k spaghetti code is hard to maintain. http://stackoverflow.com/questions/783974/how-to-implement-solid-principles-into-an-existing-project – Jordi Kroon Jan 31 '16 at 19:24
  • 1
    Some refactoring? No I'm not touching critical software just because it's spaghetti code. Not without test environment AND unit/integration tests AND that query logging. Also I'm still not sure toward what should I refractor that code. So let's focus on the question for now. Please. – przemo_li Jan 31 '16 at 19:55

1 Answers1

1

You can user SQL Server Profiler. It will be installed on the server or anywhere you have SQL Sever Management Tools installed. Normally something like this:

"C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\PROFILER.EXE"

  1. Start SQL Profiler
  2. Enter the server info, click Connect
  3. Choose a template. I suggest you choose "Duration" because by default it only logs commands that have completed.
  4. Click RUN.
  5. Click around in your application and you'll see your SQL statements execute.

If there's a lot happening on your server. You will see a TON of stuff go by and realize it's too much to comprehend. So my suggestion is:

  1. Update your connection string to include:

"Application Name=MyAppNameHere;"

  1. Before Step 4, go under the "Events Selection" tab.
  2. Check "Show all columns"
  3. Place a checkmark under "ApplicationName"
  4. Click "Column Filters" button
  5. Select "Application Name" on left.
  6. under "Like" enter "MyAppNameHere".
  7. Click Run

Now when you click around in your application it will only log things that you are doing.

You can have this data dropped to a table or a file or you can just look at what is outputted to the screen.

You may find it's helpful to add other columns or events.

Robert Paulsen
  • 4,935
  • 3
  • 21
  • 27
  • That tip with changing connection details is great! Could it work if I cloned that ODBC connection and switched connection used in script to this changed clone? It would then only log this single file, right? – przemo_li Feb 01 '16 at 07:16
  • I don't know anything php, so I'm not sure if "cloned" means something specific, but I think your answer is "yes". – Robert Paulsen Feb 01 '16 at 15:49