Jump to content

Assigning the result of sqlsrv_query to a class variable


PHPUser322

Recommended Posts

Hello,

 

I am creating a class which contains one private function that deals with connecting to a SQL Server and executing the sql that is passed in to that function.

I have defined a class variable which is assigned the value of sqlsrv_query like so.

 

$this->QueryResult = sqlsrv_query($conn,$sql);

I have placed private $QueryResult at the top of my class.

 

The other public methods call this private function to assign the result set to the class variable, the private method returns and then the public methods loop through the results array like so.

 

while($row = sqlsrv_fetch_array($this->QueryResult))

 

.. but the while loop never gets entered.

 

If I declare everything in the same method then it works, however there are going to be several public methods that will use this private method and I don't want to duplicate all the database coding.

 

Hope someone can help

 

 

Link to comment
Share on other sites

I think you want to create a method like this

 

public function setQueryResult ( $value ) {

  $this->QueryResult = $value;

}

 

then use

 

$obj->setQueryResult( sqlsrv_query($conn,$sql) );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.