PHP MS_SQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
camaan
Forum Newbie
Posts: 1
Joined: Thu Jan 20, 2011 5:34 am

PHP MS_SQL

Post by camaan »

Hello there!

I'm a novice php programmer and learning through the book "Learning PHP 5".
I just came to chapter 7 which is about DB usage. I am not entierly unknown to DB as i have been working with MS SQL through java and C#.
Though, when i try a simple piece of code to see if i can get connection to the DB:

Code: Select all

$connection = mssql_connect('SQLEXPRESS', 'sa');
if($connection)
{
	print 'Yay';
}
else{
	
	print 'Nope';
}
my XAMPP apache server crashed with a "Apache HTTP server stopped working"

I have been looking around and did enable php_mssql in php.ini file.
My server is willing to run other php scripts but just not the db connection script.

Anyone have some advice i could get to get it up and running?

Thanks in advance - Rasmus W.

PS. some system information -
OS : Vista
PHP version : 5.3
Apache version : 2.2
Editor (if that has any influence) : Eclipse Helios

PPS.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP MS_SQL

Post by social_experiment »

http://www.apachefriends.org/f/viewtopi ... 16&t=42234
This person seems to have a similar issue.
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
brothaofdes
Forum Newbie
Posts: 21
Joined: Thu Jan 20, 2011 10:05 am

Re: PHP MS_SQL

Post by brothaofdes »

After enabling the php_mssql.dll in the PHP.INI file, I left all the other attributes at their default and have no problems connecting to my MSSQL server.

Try this, a simple set of functions to get you started. Just define the SQL, and pass it empty variables for result and link, and you are good to go. Call the

Code: Select all

openRecordset($sql, $result, $link):
function calls

Code: Select all

<?php
  /******
  * This is a basic MSSQL class structure
  * All functionality is tailored to NSI requirements
  * 
  * to do: make this usable for both MYSQL and alsp MSSQL
  *******/
  
  // open and establish a connection with the testing db, simplified processing
  function connect_sql(&$link)
  {
      include (dirname(__FILE__).'/config.php');
      $link = mssql_connect($dbServer, $dbUsername, $dbPassword);
      if (!$link)die('Could not connect');
      else if(!mssql_select_db($dbDatabase))close_sql($link);
  }
// close db connection, verify that connection is closed. Repeat 5 times until closed
  function close_sql($link)
  {
      $retVal=false;
      if($link){
          if(mssql_close($link)) $retVal=true;
          else
          {
              for($i=0;$i<5;$i++)
              {
                  if(mssql_close($link)) {
                      $retVal=true;
                      $i=99;
                  }
              }
          }
      }
      return $retVal;
  }
  
  // open db and return recordset
  function openRecordset($sql, &$result, &$link)
  {
       if(!$link)connect_sql($link);
       $result = mssql_query($sql, $link);   
  }

  /*
    mssql_connect($mssql_hostname,$mssql_username,$mssql_password)

             or DIE("Database failed to respond.");

         //OK, we're connected at this point

         mssql_select_db($mssql_database)or DIE("Database unavailable");

         //the database has been selected and opened,

         // define the SQL statement

         $query = "update dbo.branchinfo

                  set bStatus= $act, bMDTType = '$stat', bChanged = 1

                  where bMCU = '$mcu' and bID = '$mid'";

         // store the results in a temporary location

         $rs = mssql_query($query);

         if(rows_affected()==1)$val=1;

         else $val=0;

         mssql_close();

  */
  ?>
the config.php file

Code: Select all

<?php
    $dbServer=<name of mssql server>;
    $dbUsername=<valid username>;
    $dbPassword=<password>;
    $dbDatabase=<database you want to connect to>;
?>
Post Reply