Solved

Why does this not work?

Posted on 2016-07-28
6
56 Views
Last Modified: 2016-08-02
Here's the function the way I run it in MSSQL Studio...

select dbo.ufn_ClientPreference( 28  , 'StatementRollUp' )

Here's the way I run it using PDO:

<?php
function test(){
	
global $mssql_pdo;	'

$sql="select dbo.ufn_ClientPreference( 28 , 'StatementRollUp' ) as StatementRollUp";

$query=$mssql_pdo->query($sql);
$row=$query->fetch_object();
	if(!$query)
	{
		echo "nope";
	}
}			

$test_stuff=test();

if($test_stuff)
{
//echo "yes";
}	
?>

<table>
	<tr>
		<td><?php echo $row['StatementRollUp'];?></td>
	</tr>
</table>

Open in new window


No specific errors, just a page that says it can't be displayed.

What's wrong?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41733039
Stray apostrophe on line 4, maybe?  Looks like a PHP parse error to me.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41733050
Also, it might be a good idea to write the function like this (fragment) that uses dependency injection instead of globals.
<?php
function test($mssql_pdo){
    $sql = "select dbo.ufn_ClientPreference( 28 , 'StatementRollUp' ) as StatementRollUp";
    $query = $mssql_pdo->query($sql);
}

Open in new window

Here's an explanation of why: https://www.experts-exchange.com/articles/19999/PHP-Design-Avoiding-Globals-with-Dependency-Injection.html
0
 

Author Comment

by:brucegust
ID: 41733070
Nuts! Ray, that was a typo. There is no stray apostrophe, I just slipped when I was copying the code.

Here's the thing: There was another scenario in my last job where, in the context of PDO, if you were interacting with a T-SQL Stored Procedure, you couldn't create a temp table within the procedure. I don't have my notes in front of me, so I'm just trying to remember. But the bottom line was that there appeared to be certain situations where if you were using PDO and calling any T-SQL functions and / or stored procedures, you needed to proceed with caution because within that dynamic you had a breakdown in communication. Does that resonate with you? Have you encountered that situation where you were using PDO to interact with a T-SQL database in a way that incorporated Stored Procedures and / or functions on the server and found that what may run without any problem in MSSQL Studio did not translate in PDO?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:brucegust
ID: 41738871
Ray, I'm going to close the question. As always, I appreciate your time. The app I'm working on is using "pconnect." How that differs from a PDO connection and whether or not that would / could make any difference in the way a stored procedure or a function on the server would fire, I don't know. I'm thinking there are other variables at work here that I'm not necessarily aware of and I was able to solve the problem that I was working on, so this is a moot point...for now, anyway.

Thanks again!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41739014
Here's pconnect() - generic, but still well-explained.
http://php.net/manual/en/function.mssql-pconnect.php

Here's connect() - not persistent.
http://php.net/manual/en/function.mssql-connect.php

Why prefer connect() over pconnect()?  If you're using pconnect() and anything goes wrong in another script using the connection that causes any kind of damage to the connection, you may experience intermittent run-time failures that cannot be replicated.  Professionals do not like to debug those sorts of things; we prefer ways of working that are stable and predictable, with a minimum of "spooky action at a distance."  If you still think there is an advantage to pconnect() consider attaching a script timer to the processes and see if pconnect() saves you any noticeable amount of script time.
0
 

Author Comment

by:brucegust
ID: 41739203
I'm getting to a place where I think some programmers simply default to what they're most familiar with rather than venturing beyond the reservation and use something else that may be more efficient.

In this instance, based on the explanations you've referred as well as some other things I've read, it looks as though pconnect may be the better option, but I'm still suspicious that while PHP and T-SQL can get along, there are some anomalies that represent some "quirks," so even when the PHP code is sound, you don't always get the recordset you expect. Not a dealbreaker necessarily, but something to be sensitive when attempting to troubleshoot some code.

Yes?
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question