Hi, I'm trying to include a file which contains a simple db connection script. But it keeps giving me the error no db selected, as if the file was never included. Here are both my files. Do you see anything wrong?

mainfile.php

include("functions/db_conn.php");

	// Now check to see if there is someone with that user name already in the database
	$check_name = "select * from user where user_name = '$user_n'";
	$result = mysql_query($check_name);

	if (!$result)
	{
		die('Invalid query: ' . mysql_error());
	}

db_conn.php

<?php
function ConnectToDb()
{
//connect to the db server
$conn = mysql_connect('localhost', 'victorb_victor', 'mrbo');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
//successfully connected to server

	// make victorb17 the current db
	$selected_db = mysql_select_db('victorb_victorb17', $conn);
	if (!$selected_db) 
	{
		die ('Can\'t use database! : ' . mysql_error());
	}
	//connected to database
	} 

?>

Thanks!

    From what you've shown you havent called for the function however have included the file. Try making a call to the function to open a connection to the database.

      planetsim's solution is bang-on, but in this case, you probably don't the function at all - unless you want to pass a variable that determines which db to connect to, or if db_conn.php might do other things, etc.

      Also, might I suggest that you use the

      tags in the future so your code is more readable...

        I tried calling the function like this: ConnectToDb();

        It says Fatal Error: Call to undefined function ConnectToDb()

        Do I need to use a function prototype like in C? if so is it the same syntax? Thanks!

          No function prototypes needed.
          In fact, in PHP4 onwards you can even use a function before defining it, as long as it is defined.

          Test by including the file and calling the function in a new test page.
          For this test page, drop out everything else.

            Ok I made this test page:

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>Untitled Document</title>
            </head>
            <body>
            <?php
            include("http://72.9.224.178/~victorb/functions/db_conn.php");
            ConnectToDb();
            ?>
            </body>
            </html>

            When I test it, it still says:

            Fatal error: Call to undefined function: connecttodb() in /test.php on line 14

            Thanks!

              Dont include an absolute URI.
              More likely it is something along the lines of:
              include "../../functions/db_conn.php";

                Well on the test page, using relative url worked. I think my problem is in my site structure:

                _root
                |
                -functions //where the database connection function is
                |
                -accounts //where the calling script is from

                if I put them in the same dir it works fine. only if i do it like above. is there any way to use my structure from above?

                when I have them in the structure above, I get these error messages:

                Warning: main(functions/db_conn.php): failed to open stream: No such file or directory in /home/victorb/public_html/login_and_register/register2.php on line 206

                Warning: main(functions/db_conn.php): failed to open stream: No such file or directory in /home/victorb/public_html/login_and_register/register2.php on line 206

                Warning: main(): Failed opening 'functions/db_conn.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/victorb/public_html/login_and_register/register2.php on line 206

                Fatal error: Call to undefined function: connecttodb() in /home/victorb/public_html/login_and_register/register2.php on line 207

                Thanks!

                  if both folders are in the ROOT directory as seperate paths (e.g. public_html/functions & public_html/accounts - it should be fine...

                  the include should only have to be like this:

                  include 'functions/dbconnect.php';

                  you still need to call the "functions" folder before the file since it's in another area... don't use a trailing slash such as "/functions".

                  so your directory should look like the following i guess?

                  ROOT
                  |
                  functions - dbconnect.php
                  |
                  applications

                  this is how you want it correct?

                  if you have it deeper into the directory you'll need to specify it as Laserlight said.

                    also, why not try Classes instead?

                    dbSetup.php

                    <?php
                    
                     class dbInfo {
                    
                    var $dbSetup;
                    
                     function dbSetup() {
                    
                       $dbSetup['host'] = 'localhost';
                       $dbSetup['username'] = 'uername';
                       $dbSetup['password'] = 'pass';
                       $dbSetup['dbname'] = 'yourDBName';
                    
                       return $dbSetup;
                      }
                    
                    }
                    
                    ?>
                    
                    
                    

                    something similar to that...

                    and you can extend from that for other classes... connections, etc...

                    just a thought.

                      might be better to use the '../' method (as laserlight suggested) so that the folders do not necessarily have to be in the root.
                      Your include would then look like this:
                      include ('../'functions/dbconnect.php');

                      the '..' means 'parent'

                        Originally posted by Clenard
                        dbSetup.php

                        <?php
                        
                         class dbInfo {
                        
                        var $dbSetup;
                        
                         function dbSetup() {
                        
                           $dbSetup['host'] = 'localhost';
                           $dbSetup['username'] = 'uername';
                           $dbSetup['password'] = 'pass';
                           $dbSetup['dbname'] = 'yourDBName';
                        
                           return $dbSetup;
                          }
                        
                        }
                        
                        ?>

                        [/B]

                        In my opinion, hardcoding constants into functions and classes is bad practise. You lose the portability writing a class gives you. If your class does nothing more than defining a few variables, don't use a class.

                          Write a Reply...