Solved

PHP newbie retrieve from SQL Server Question

Posted on 2013-11-04
5
535 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
  • 2
5 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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 42

Accepted Solution

by:
Chris Stanyon earned 150 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 108

Expert Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now