Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP database selection issue
    primarykey
    data
    text
    <p>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!</p> <p><strong>Files</strong></p> <ul> <li>config.php</li> <li>database.php</li> <li>news.php</li> <li>BLnews.php</li> <li>index.php</li> </ul> <p><strong>Includes</strong></p> <ul> <li>config.php -> news.php</li> <li>database.php -> news.php</li> <li>news.php -> BLnews.php</li> <li>BLnews.php -> index.php</li> </ul> <p>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!</p> <p><strong>config.php</strong></p> <pre><code>&lt;?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "test"; ?&gt; </code></pre> <p><strong>database.php</strong></p> <pre><code>&lt;?php class Database { //------------------------------------------- // Connects to the database //------------------------------------------- function connect() { if (isset($dbhost) &amp;&amp; isset($dbuser) &amp;&amp; isset($dbpass) &amp;&amp; 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 ?&gt; </code></pre> <p><strong>News.php</strong></p> <pre><code>&lt;?php // include the config file and database class include 'config.php'; include 'database.php'; ... ?&gt; </code></pre> <p><strong>BLnews.php</strong></p> <pre><code>&lt;?php // include the news class include 'news.php'; // create an instance of the Database class and call it $db $db = new Database; $db -&gt; 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; } ?&gt; </code></pre> <p><strong>index.php</strong></p> <pre><code>&lt;?php ... include 'includes/BLnews.php'; $blNews = new BLnews(); $news = $blNews-&gt;getNews(); ?&gt; ... &lt;?php while($row = mysql_fetch_array($news)) { echo '&lt;div class="post"&gt;'; echo '&lt;h2&gt;&lt;a href="#"&gt; ' . $row["title"] .'&lt;/a&gt;&lt;/h2&gt;'; echo '&lt;p class="post-info"&gt;Posted by &lt;a href="#"&gt; &lt;/a&gt; | &lt;span class="date"&gt; Posted on &lt;a href="#"&gt;' . $row["date"] . '&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;'; echo $row["content"]; echo '&lt;/div&gt;'; } ?&gt; </code></pre> <p>Well this is pretty much everything that should get the information going however due to the mysql_error in <code>$result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());</code> I can see the error and it says: </p> <blockquote> <p>Could not execute query. Reason: No database selected</p> </blockquote> <p>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!</p> <p>Greets</p> <p>Lemon</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload