Solved

PHP newbie retrieve from SQL Server Question

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.

728 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