Jump to content

Another n00bish PHP SQL question -_-


riT-k0MA

Recommended Posts

Hi again.

 

I learn new code best by getting an example from somewhere and studying it. It's probably not the best way nut it works for me.

Unfortunately, if the example code doesn't work I have no idea how to fix it.

 

I got this from here

 

<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
     echo "Could not connect.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Define the query. */
$tsql = "SELECT ProductID,
                UnitPrice,
                StockedQty 
         FROM Purchasing.PurchaseOrderDetail
         WHERE StockedQty < 3 
         AND DueDate='2002-01-29'";

/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
     echo "Statement executed.\n";
} 
else 
{
     echo "Error in statement execution.\n";
     die( print_r( sqlsrv_errors(), true));
}

/* Iterate through the result set printing a row of data upon each
iteration.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
     echo "ProdID: ".$row[0]."\n";
     echo "UnitPrice: ".$row[1]."\n";
     echo "StockedQty: ".$row[2]."\n";
     echo "-----------------\n";
}

/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>

 

Obviously I changed the DB name and fields and datatypes.

 

It worked perfectly fine yesterday.

I tried to use a modified version of it this morning and it didn't work.

I then tried the origional (slightly modified) version of the code. That didn't work.

 

Both times the actual PHP code shows after " $connectionInfo = array( "Database"=> "  no matter what's in front of the >  ???

 

I really have no clue about what's going on and would be greatly enlightened and appreciative if someone could point out the fup.

 

many Thanks in advance

riT-k0MA

Link to comment
Share on other sites

I learn new code best by getting an example from somewhere and studying it. It's probably not the best way nut it works for me.

Unfortunately, if the example code doesn't work I have no idea how to fix it.

 

That is indeed the biggest problem with simply copying and pasting code.

 

There are no such functions as all these sqlsrv* functions built into php. Have you defined them somewhere?

Link to comment
Share on other sites

Then I would suggest that could be your problem. Still seems odd though. However, no matter how you look at it that code is useless without those functions and will need to be rewritten using a proper (existing) database abstraction layer.

Link to comment
Share on other sites

I'm using the PHP docuentation, though as far as I can see, it's or nromal SQL authentication.  Unfortunatly I have to use Windows Authentication, which is why I was using the msdn stuff.

 

Looks like I'll have to try and muddle through everything

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.