Solved

PHP newbie retrieve from SQL Server Question

Posted on 2013-11-04
5
539 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 110

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 43

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 110

Expert Comment

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

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

726 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