Setup Problem

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

I setup a test box on my computer. This is Windows XP, SP3 running MS SQL Express and IIS 5.1. I installed PHP 5.2.10 and it works great! If I write a script that looks like this:

Code: [ Select ]
<?php

define("DB_SERVER", "computer\sqlexpress,1433");
define("DB_USER", "user");
define("DB_PASS", "password");
define("DB_NAME", "database");
?>
<?php
    $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
        if(!$connection)
        {
        die('Something went wrong while connecting to MSSQL');
        }
    $db_select = mssql_select_db(DB_NAME, $connection);
    if (!$db_select){
        die("Database selection failed");
    }
?>
<?php
$test = mssql_query("SELECT * from table", $connection);
                if (!$test) {
                    die("Database query failed: " . mssql_error());
                }         
                while ($test_sel = mssql_fetch_array($test)) {
                    echo "{$test_sel['svr_name']}";
                }
?>
  1. <?php
  2. define("DB_SERVER", "computer\sqlexpress,1433");
  3. define("DB_USER", "user");
  4. define("DB_PASS", "password");
  5. define("DB_NAME", "database");
  6. ?>
  7. <?php
  8.     $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
  9.         if(!$connection)
  10.         {
  11.         die('Something went wrong while connecting to MSSQL');
  12.         }
  13.     $db_select = mssql_select_db(DB_NAME, $connection);
  14.     if (!$db_select){
  15.         die("Database selection failed");
  16.     }
  17. ?>
  18. <?php
  19. $test = mssql_query("SELECT * from table", $connection);
  20.                 if (!$test) {
  21.                     die("Database query failed: " . mssql_error());
  22.                 }         
  23.                 while ($test_sel = mssql_fetch_array($test)) {
  24.                     echo "{$test_sel['svr_name']}";
  25.                 }
  26. ?>


This works perfectly, the query runs, no problem at all. However, if I write that same script like this:
Code: [ Select ]
<?php require_once("connection.php"); ?>
<?php
$test = mssql_query("SELECT * from table", $connection);
                if (!$test) {
                    die("Database query failed: " . mssql_error());
                }         
                while ($test_sel = mssql_fetch_array($test)) {
                    echo "{$test_sel['svr_name']}";
                }
?>
  1. <?php require_once("connection.php"); ?>
  2. <?php
  3. $test = mssql_query("SELECT * from table", $connection);
  4.                 if (!$test) {
  5.                     die("Database query failed: " . mssql_error());
  6.                 }         
  7.                 while ($test_sel = mssql_fetch_array($test)) {
  8.                     echo "{$test_sel['svr_name']}";
  9.                 }
  10. ?>

I have all my database connection info in the connection.php file. For some reason, it's completely skiping over the require_once statement and then of course, my query will not run. Any suggestions? I've used this same code many times, so I know the syntax is correct, but for some reason, maybe the way I have PHP setup or what, but it just is not reading any type of include statement.

Thanks for any help I can get!

Jordon
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Hi jordanshaw, just a quick suggestion if you don't mind. Why not change your database connection file into a class? Make a class that defines your connection strings on instantiation and then have a connect() function that you can call from other pages?

You could get what you have working, but I highly recommend that you look into Object-Oriented PHP and get away from the procedural look that you've got now.
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Great suggestion and I might look into that; however, that still doesn't fix the fact that my require_once fuction isn't working. I can create another file that just echo's Hello and put a include statement at the top and it won't read it. Its like it completely skips anytype of include statement.

Thanks,
Jordon
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Can you post your connection.php or is that the top code section?
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

The top code is when I put the connection.php with the test.php and it works perfectly. Then I remove the connection peice and put it in the connection.php file. It looks like this:

Code: [ Select ]
<?php

define("DB_SERVER", "computer\sqlexpress,1433");
define("DB_USER", "user");
define("DB_PASS", "password");
define("DB_NAME", "database");
?>
<?php
    $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
        if(!$connection)
        {
        die('Something went wrong while connecting to MSSQL');
        }
    $db_select = mssql_select_db(DB_NAME, $connection);
    if (!$db_select){
        die("Database selection failed");
    }
?>
  1. <?php
  2. define("DB_SERVER", "computer\sqlexpress,1433");
  3. define("DB_USER", "user");
  4. define("DB_PASS", "password");
  5. define("DB_NAME", "database");
  6. ?>
  7. <?php
  8.     $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
  9.         if(!$connection)
  10.         {
  11.         die('Something went wrong while connecting to MSSQL');
  12.         }
  13.     $db_select = mssql_select_db(DB_NAME, $connection);
  14.     if (!$db_select){
  15.         die("Database selection failed");
  16.     }
  17. ?>
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Is there some configuration that allows you to turn the include|include_once|require|require_once statements off/on?

If so, make sure yours is on.

As for connection classes, there are a ton of those that are already created. I have mine up on phpclasses.org. There are also loads of other DB Access Wrappers and other fun classes as well :lol: Take a peek.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

I might be taking your knowlede for granted here and please don't be offended if you know what I am saying. Not trying to make you feel silly just trying to help you debug.

First inside your php.ini file set error_reporting = E_ALL.

Then read on:

Code: [ Select ]
2.<?php require_once("connection.php");
3.
4.$test = mssql_query("SELECT * from table", $connection);
5.              if (!$test) {
6.                  die("Database query failed: " . mssql_error());
7.              }         
8.              while ($test_sel = mssql_fetch_array($test)) {
9.                  echo "{$test_sel['svr_name']}";
10.              }
11.?>
  1. 2.<?php require_once("connection.php");
  2. 3.
  3. 4.$test = mssql_query("SELECT * from table", $connection);
  4. 5.              if (!$test) {
  5. 6.                  die("Database query failed: " . mssql_error());
  6. 7.              }         
  7. 8.              while ($test_sel = mssql_fetch_array($test)) {
  8. 9.                  echo "{$test_sel['svr_name']}";
  9. 10.              }
  10. 11.?>


Remove the additional code block. Each block incurs a proccessing penalty. Make sure that the path to the file is correct. try include and include_once. I personally have never used require_once.

PHP should fail if it cannot include the file. are you certain you have delimited the file with the <?php signatures?

connection.php
Code: [ Select ]
1.<?php
2.
3.
4.define("DB_SERVER", "computer\sqlexpress,1433");
5.define("DB_USER", "user");
6.define("DB_PASS", "password");
7.define("DB_NAME", "database");
8.
9.
10. $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
11.      if(!$connection)
12.      {
13.      die('Something went wrong while connecting to MSSQL');
14.      }
15. $db_select = mssql_select_db(DB_NAME, $connection);
16. if (!$db_select){
17.      die("Database selection failed");
18. }
19.?>
  1. 1.<?php
  2. 2.
  3. 3.
  4. 4.define("DB_SERVER", "computer\sqlexpress,1433");
  5. 5.define("DB_USER", "user");
  6. 6.define("DB_PASS", "password");
  7. 7.define("DB_NAME", "database");
  8. 8.
  9. 9.
  10. 10. $connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
  11. 11.      if(!$connection)
  12. 12.      {
  13. 13.      die('Something went wrong while connecting to MSSQL');
  14. 14.      }
  15. 15. $db_select = mssql_select_db(DB_NAME, $connection);
  16. 16. if (!$db_select){
  17. 17.      die("Database selection failed");
  18. 18. }
  19. 19.?>


test.php
Code: [ Select ]
1.
2.<?php
3. require_once("connection.php");
4.$test = mssql_query("SELECT * from table", $connection);
5.              if (!$test) {
6.                  die("Database query failed: " . mssql_error());
7.              }         
8.              while ($test_sel = mssql_fetch_array($test)) {
9.                  echo "{$test_sel['svr_name']}";
10.              }
11.?>
  1. 1.
  2. 2.<?php
  3. 3. require_once("connection.php");
  4. 4.$test = mssql_query("SELECT * from table", $connection);
  5. 5.              if (!$test) {
  6. 6.                  die("Database query failed: " . mssql_error());
  7. 7.              }         
  8. 8.              while ($test_sel = mssql_fetch_array($test)) {
  9. 9.                  echo "{$test_sel['svr_name']}";
  10. 10.              }
  11. 11.?>


I must add a few things. A class won't work if you can't include it. Not sure why you defining the values as constants but each to his own :)

Check the above and let me know if it matches your code. Just because the file is called .php doesn't mean it will be processed as php. It has to have the designated <?php ?> tags.

Let me know if this produces any errors

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 53 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.