[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PHP newbie retrieve from SQL Server Question

Posted on 2013-11-04
5
Medium Priority
?
542 Views
Last Modified: 2013-11-04
I need a bit of a steer in the right direction here:

I have YEARS of classic ASP experience and am now learning PHP.
I am reading tutorials etc but nothing is making sense. I think I'm still 'thinking' in classic ASP terms.

I have the following connection:

$connectionOptions = array( "Database"=>"mydatabase", "UID"=>"myusername", "PWD"=>"mypassword");
$conn = sqlsrv_connect("99.999.999.999,1433", $connectionOptions);
$SQLStmt = "SELECT * FROM Speaker";
$params = array();
$RS_Speaker01 = sqlsrv_query($conn,$SQLStmt,$params);

Open in new window


It seems to connect just fine but I get a message saying:

PHP Notice: Undefined index: SpeakerId in E:\mypath\test.php on line 20 PHP Warning: sqlsrv_query() expects parameter 1 to be resource, boolean given in E:\mypath\test.php on line 21

Any help pointing me in the right direction would be greatly appreciated...

Regards,
0
Comment
Question by:splanton
[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
  • 2
5 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 39621691
This almost certainly means the connect failed.  Try printing the value of $conn with var_dump().

With ASP experience you will pick up PHP easily.  There are some good learning resources in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 600 total points
ID: 39621727
Have a look at this. It checks for a succesful connection before continuing:

$connectionOptions = array( "Database"=>"mydatabase", "UID"=>"myusername", "PWD"=>"mypassword");
$conn = sqlsrv_connect("99.999.999.999,1433", $connectionOptions);

if( $conn === false ) {
    die("Connection Failed");
}

$SQLStmt = "SELECT * FROM Speaker";
$RS_Speaker01 = sqlsrv_query($conn, $SQLStmt);

while ($row = sqlsrv_fetch_object($RS_Speaker01)) {
   //look at each row
   var_dump($row);
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:splanton
ID: 39621879
Thanks for the heads up on the dump variables etc. Had to go round the houses and double check everything.
It was a connection issue. The dump gave me a clearer idea what was happening. had to install 2012 native connection dll and all was OK.

Retrieving data like a pro again! :)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39621911
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

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!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

649 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