Yes.

I generally just do something like this:

includes.inc.php

<?php
include('class_db.inc.php');

$myDb = new dbConnection;
$myDb->dbLoc = "localhost";
$myDb->dbUser = "3rd_ard";
$myDb->dbPass = "xyz";
$myDb->dbName = "abc_123";
$myDb->dbConnect();

?>

index.php

<?php
include('includes.inc.php');

$dblist = $myDb->dbFetch(mysql_list_dbs());

foreach($dblist as $key=>$val){
   echo $val.'<br>';
}
?>

But that's just an example using my database class.

    May I ask why you call it includes.inc.php? Why the two decimal places? I always use one just in case!

      It is to point out that the file is to be included, not loaded directly.

      Not neccesary as I include just .php files all the time, it just lets me and PHP know that it is an include file.

        Ok. I am trying the include statement but it doesn't seem to be working very well on the page I am trying it on. The page is shown below:

        addsent.php

        <?
        include('includes-db-path.php');
        mysql_query("insert into Articles(TopicName, MetaTitle, MetaDesc, TopicTitle, TopicBody, Advert) values('$TopicName', '$MetaTitle', '$MetaDesc', '$TopicTitle', '$TopicBody', '$Advert')"); 
        mysql_close($dbh);
        header("Location: list.php"); 
        ?>

        includes-db-path.php

        <?
        $dbh=mysql_connect ("localhost", "alwaysac_Jon", "Angie") or die ('I cannot connect to the database because: ' . mysql_error());
        mysql_select_db ("alwaysac_MyArticles"); 
        ?>

        What is wrong here?

          You set $dbh to the database resource, but you never actually used it.

          Take out the $dbh = part, or add that resouce identifier to your mysql_query.

            Sorry, but I'm relatively new at this. Not quite sure what you mean??

              No problem. 🙂

              Here's what's wrong:

              You setup your connection on variable $dbh.

              Until you call $dbh, it's just that...a variable.

              In order to activate it, you would need to use it as a resource identifier by calling your query like so:

              mysql_query("SELECT * FROM table", $dbh);

              That let's PHP know to preform the query with resource $dbh, which is your connection.

              That just whats wrong and A method to fix it.

              Now, I personally never have the need to connect to more than one server or database, so I usualy don't make my connection a variable, for lack of better words.

              What I generally do is just this:

              includes.php

              <?php
              mysql_connect("localhost", "dbuser", "dbpass");
              mysql_select_db("database");
              ?>

              index.php

              <?php
              include('includes.php');
              
              $stuff = mysql_query("SELECT stuff FROM table");
              $iGotIt = mysql_fetch_array($stuff);
              
              echo $iGotIt['stuff'];
              ?>

              You may have noticed that I didnt use a resource identifier on my query. That's becuase if you dont include one, than the last know database connection is used. Which happens to be in the include file.

              Hope that makes some sense. 🙂

                Yes, I removed it and it worked. Except for one thing...

                I use mysql_close($dbh); at the end of each PHP section on my pages. Now it says:

                Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/alwaysac/public_html/Dynamic Site/edit.php on line 18

                Is there a way to close any MySQL without providing a specific parameter? i.e. can I just use:

                mysql_close();

                ????

                  Originally posted by Jon12345
                  can I just use:

                  mysql_close();

                  ????

                  Try it

                  If you computer explodes I will buy you a new one

                    Yep. When you call mysql_close without a resource identifier, it defaults to the last know connection.

                    Also, although it is good practice to close your connections on lengthy scripts or script with multiple connections, it is not needed as the connection is automatically closed when the script finishes/page loads.

                      Are you saying that really I don't actually need a mysql_close() anyway?

                        Ok, thanks. I've made those modifications. But I am still getting an error here:

                        addsent.php

                        <?
                        include('includes-db-path.php');
                        mysql_query("insert into Articles(TopicName, MetaTitle, MetaDesc, TopicTitle, TopicBody, Advert) values('$TopicName', '$MetaTitle', '$MetaDesc', '$TopicTitle', '$TopicBody', '$Advert')"); 
                        mysql_close();
                        header("Location: list.php"); 
                        ?>

                        includes-db-path.php

                        <?
                        mysql_connect ("localhost", "alwaysac_Jon", "Angie") or die ('I cannot connect to the database because: ' . mysql_error());
                        //$dbh=mysql_connect ("localhost", "alwaysac_Jon", "Angie") or die ('I cannot connect to the database because: ' . mysql_error());
                        mysql_select_db ("alwaysac_MyArticles"); 
                        ?>

                        Am I still missing something or misunderstood?

                          What's the error?

                          My guess is the whitespace between mysql_connect () and the other.

                            Ahh, my mistake. The redirect was to a page where the name had changed. I forgot to update it.

                            Thanks for all your help on this. I've learn't a lot and it will be much quicker for me to code now. 🙂

                              No problem. 🙂

                              If you don't have any more questions, please mark this thread resolved with the link at the bottom of your page.

                                Write a Reply...