0

I'm in a bit of a pickle with freshening up my PHP a bit, it's been about 3 years since I last coded in PHP. Any insights are welcomed! I'll give you as much information as I possibly can to resolve this error so here goes!

Files

  • config.php
  • database.php
  • news.php
  • BLnews.php
  • index.php

Includes

  • config.php -> news.php
  • database.php -> news.php
  • news.php -> BLnews.php
  • BLnews.php -> index.php

Now the problem with my current code is that the database connection is being made but my database refuses to be selected. The query I have should work but due to my database not getting selected it's kind of annoying to get any data exchange going!

config.php

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
?>

database.php

<?php
class Database {

    //-------------------------------------------
    //    Connects to the database
    //-------------------------------------------
    function connect() {
        if (isset($dbhost) && isset($dbuser) && isset($dbpass) && isset($dbname)) {
            $con = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: " . mysql_error());
            $selected_db = mysql_select_db($dbname, $con) or die("Could not select test DB");
        }
    }// end function connect
} // end class Database
?>

News.php

<?php
// include the config file and database class
include 'config.php';
include 'database.php';

...
?>

BLnews.php

<?php
// include the news class
include 'news.php';
// create an instance of the Database class and call it $db
$db = new Database;
$db -> connect();

class BLnews {

    function getNews() {
        $sql = "SELECT * FROM news";
        if (isset($sql)) {
            $result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());
        }
        return $result;
    }
?>

index.php

<?php
...

include 'includes/BLnews.php';
$blNews = new BLnews();
$news = $blNews->getNews();
?>

...
<?php 
        while($row = mysql_fetch_array($news))
        {
        echo '<div class="post">';
        echo '<h2><a href="#"> ' . $row["title"] .'</a></h2>';
        echo '<p class="post-info">Posted by <a href="#"> </a> | <span class="date"> Posted on <a href="#">' . $row["date"] . '</a></span></p>';
        echo $row["content"];
        echo '</div>';
        }
        ?>

Well this is pretty much everything that should get the information going however due to the mysql_error in $result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error()); I can see the error and it says:

Could not execute query. Reason: No database selected

I honestly have no idea why it would not work and I've been fiddling with it for quite some time now. Help is most welcomed and I thank you in advance!

Greets

Lemon

12
  • 1
    Since you've been out of PHP for a little while, you should google PDO. Commented Nov 30, 2011 at 1:51
  • Sorry but in your database class I don't see where you define vars as $dbhost or $dbuser. In the case you didn't, the if will be always false so you'll neither get a connection nor a die(). Commented Nov 30, 2011 at 1:54
  • @AurelioDeRosa: He says that the connection is being made, and if you look in his code, the connection will only be made if those variables are all set. Commented Nov 30, 2011 at 1:56
  • @Cyclone How can he surely assert this if he don't define those vars? Commented Nov 30, 2011 at 1:57
  • I added what's in config.php as well now, maybe it's easier for people to see everything then! Thanks for the replies! Commented Nov 30, 2011 at 1:58

2 Answers 2

1

The values you use in your functions aren't set with a value. You likely need to convert the variables used to $this->dbName etc or otherwise assign values to the variables used.

Edit for users comment about variables defined in config.php:

You really should attempt to get the data appropriate for each class inside that class. Ultimately your variables are available to your entire app, there's no telling at this point if the variable was changed by a file including config.php but before database.php is called.

I would use a debugging tool and verify the values of the variables or just var_dump() them before the call.

Sign up to request clarification or add additional context in comments.

5 Comments

Presumably they're defined in config.php...which if you notice, is included prior to database.php
^this, they are in config.php
Then I would ensure they are truly defined in config.php. The most likely cause is that the isset() checks are failing and the mysql_select_db function is not called.
@CharlesSprayberry: But OP says he's sure the connection is made: Now the problem with my current code is that the database connection is being made but my database refuses to be selected.
Again I counter with, what is the value held in $dbName? If there is indeed a value then what are the error logs saying?
0

Your Database class methods connect and selectDb try to read from variables that are not set ($dbhost, $dbname, $con, etc). You probably want to pass those values to a constructor and set them as class properties. Better yet, look into PDO (or an ORM) and forget creating your own db class.

2 Comments

I don't think he should necessarily forget about making the DB class. It is fairly straightforward to make a MySQLi/PDO connection wrapper and can be a good learning experience. He should look into an alternative to mysql_ though.
@CharlesSprayberry you may be right. However, it does not appear to me that his Database class offers any improvement over what PDO already offers. What might be more useful to him is to create a db connection manager class (handling pdo object(s)). But, I guess it all depends on what he is trying to achieve.

Your Answer

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