• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

PHP newbie retrieve from SQL Server Question

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
splanton
Asked:
splanton
  • 2
2 Solutions
 
Ray PaseurCommented:
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
 
Chris StanyonCommented:
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
 
splantonAuthor Commented:
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
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now