-1

I have recently started to upgrade my intranet server to PHP but i am having trouble getting the code to work.

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" x-undefined>
  </head>
  <body>
    <!-- php data test -->
    <meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0">
    <meta http-equiv="expires" content="Sat, 31 Oct 2014 00:00:00 GMT">
    <meta http-equiv="pragma" content="no-cache">
  </body>
</html>

<?php
   // Connect to MSSQL and select the database
   mssql_connect('intranet', 'sa', 'bobby123');
   mssql_select_db('phptable');

   // Send a select query to MSSQL
   $query = mssql_query('SELECT * FROM [php].[dbo].[persons]');

   // Construct table
   echo '<h3>Table structure for \'persons\'</h3>';
   echo '<table border="1">';

   // Table header
   echo '<thead>';
   echo '<tr>';
   echo '<td>UserID</td>';
   echo '<td>Username</td>';
   echo '<td>Password</td>';
   echo '</tr>';
   echo '</thead>';

   // Dump all fields
   echo '<tbody>';

   for ($i = 0; $i < mssql_num_fields($query); ++$i) {
      // Fetch the field information
      $field = mssql_fetch_field($query, $i);

      // Print the row
      echo '<tr>';
      echo '<td>' . $field->userid . '</td>';
      echo '<td>' . $field->username . '</td>';
      echo '<td>' . $field->password . '</td>';
      echo '</tr>';
   }

   echo '</tbody>';
   echo '</table>';

   // Free the query result
   mssql_free_result($query);
?> 

I am using PHP 5.4 and have also tried to use 5.5 Is this something in my code, or do i need to install a plugin or something onto my server? (Windows Server 2012 R2)

4
  • I reccomend you to start with a simpler test just like <?php echo '<p>Hello World</p>'; ?> and if it works then go to more complicated tests.
    – jcbermu
    Jun 19, 2015 at 9:59
  • the simple hello world displays the page perfectally? Jun 19, 2015 at 10:17
  • perfectly (it wont let me edit it...) Jun 19, 2015 at 10:40
  • 1
    Did you even read the PHP documentation on the MSSQL interface? This is wrong in almost every way it could be. I’ll point you at a single function: error_reporting. After setting that, RTFM (there are complete examples!) and use Stack Overflow.
    – Daniel B
    Jun 19, 2015 at 11:14

2 Answers 2

0

There is no need for any extra plugin. Don't remove exceptions from your code. If it is there u can simply find position of error.

Change statements like

mssql_connect('intranet', 'sa', 'bobby123');

to

mssql_connect('intranet', 'sa', 'bobby123') or die('Something went wrong while connecting to MSSQL');

Is intranet a domian name?

1
0

It might be some Database connectivity problem.

Obtain the exact Host name, Database name, User name & Password and name of the particular Table Name.

Make your code like as below

$connect = mssql_connect('hostname','username','password');
$db = mssql_select_db('database');

Also please add mysql_error () function to identify the error.

Give the exact table name in the SQL statement instead of [php].[dbo].[persons]

1
  • that made no change to anything. Except you need to add a ; at the end of mssql_error() because it is a function Jun 19, 2015 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.