+ Reply to Thread
Results 1 to 5 of 5

Thread: Could not select database. No database selected issue

  1. #1
    sxm65 is offline x10Hosting Member sxm65 is an unknown quantity at this point
    Join Date
    May 2011
    Posts
    17

    Could not select database. No database selected issue

    Hello , i´m making a news cms for a client that want to be enable to enter a page where he can add news and other updates the code is done but i got problem that it could not select database that is zyry_nattlir. Here is the codes:

    Code:
    <?php
    
    class simpleCMS {
    
      var $host;
      var $username;
      var $password;
      var $table;
    
      public function display_public() {
        $q = "SELECT * FROM zyry_nattlir ORDER BY created DESC LIMIT 3";
        $r = mysql_query($q);
    
        if ( $r !== false && mysql_num_rows($r) > 0 ) {
          while ( $a = mysql_fetch_assoc($r) ) {
            $title = stripslashes($a['title']);
            $bodytext = stripslashes($a['bodytext']);
    
            $entry_display .= <<<ENTRY_DISPLAY
    
        <div class="post">
        	<h2>
        		$title
        	</h2>
    	    <p>
    	      $bodytext
    	    </p>
    	</div>
    
    ENTRY_DISPLAY;
          }
        } else {
          $entry_display = <<<ENTRY_DISPLAY
    
        <h2> This Page Is Under Construction </h2>
        <p>
          No entries have been made on this page. 
          Please check back soon, or click the
          link below to add an entry!
        </p>
    
    ENTRY_DISPLAY;
        }
        $entry_display .= <<<ADMIN_OPTION
    
        <p class="admin_link">
          <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
        </p>
    
    ADMIN_OPTION;
    
        return $entry_display;
      }
    
      public function display_admin() {
        return <<<ADMIN_FORM
    
        <form action="{$_SERVER['PHP_SELF']}" method="post">
        
          <label for="title">Title:</label><br />
          <input name="title" id="title" type="text" maxlength="150" />
          <div class="clear"></div>
         
          <label for="bodytext">Body Text:</label><br />
          <textarea name="bodytext" id="bodytext"></textarea>
          <div class="clear"></div>
          
          <input type="submit" value="Create This Entry!" />
        </form>
        
        <br />
        
        <a href="display.php">Back to Home</a>
    
    ADMIN_FORM;
      }
    
      public function write($p) {
        if ( $_POST['title'] )
          $title = mysql_real_escape_string($_POST['title']);
        if ( $_POST['bodytext'])
          $bodytext = mysql_real_escape_string($_POST['bodytext']);
        if ( $title && $bodytext ) {
          $created = time();
          $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";
          return mysql_query($sql);
        } else {
          return false;
        }
      }
    
      public function connect() {
        mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
        mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
    
        return $this->buildDB();
      }
    
      private function buildDB() {
        $sql = <<<MySQL_QUERY
    CREATE TABLE IF NOT EXISTS testDB (
    title		VARCHAR(150),
    bodytext	TEXT,
    created		VARCHAR(100)
    )
    MySQL_QUERY;
    
        return mysql_query($sql);
      }
    
    }
    
    ?>
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        
        <title>Simple CMS with PHP</title>
        
        <link rel="stylesheet" type="text/css" href="style.css" />
      </head>
    
      <body>
      	<div id="page-wrap">
        <?php
        
          include_once('_class/simpleCMS.php');
          $obj = new simpleCMS();
    
    	  
          $obj->host = 'localhost';
          $obj->username = 'zyry';
          $obj->password = '*****';
          $obj->table = 'zyry_nattlir';
          $obj->connect();
        
          if ( $_POST )
            $obj->write($_POST);
        
          echo ( $_GET['admin'] == 1 ) ? $obj->display_admin() : $obj->display_public();
        
        ?>
    	</div>
      </body>
    
    </html>

    Please note: I have removed the password to the db upon request from client!


    Edit: Here is the website the scripts are running on : http://clickandstream.elementfx.com/...MS/display.php
    Thanks in Advance


    Fredrik Wirth

    ---------- Post added at 08:12 PM ---------- Previous post was at 06:00 PM ----------

    okey fixed the first error that said could not select database but now im getting a access denied message how do i solve that?
    Last edited by sxm65; 07-04-2011 at 01:49 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Could not select database. No database selected issue

    Start with the X10 wiki article on MySQL connection errors.

    The mysql extension is outdated and twice supplanted. You should be using PDO. Among other reasons, it supports prepared statements.

    When asking for help with an error, always give the exact error message. Make sure you follow the instructions in my sig for more guidelines.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    sxm65 is offline x10Hosting Member sxm65 is an unknown quantity at this point
    Join Date
    May 2011
    Posts
    17

    Re: Could not select database. No database selected issue

    hey , and thanks for taking your time to help me solve my problem the error message can be seen here : http://nattlir.co.cc/SimpleCMS/display.php , it says :

    Could not select database. Access denied for user 'zyry_nattdb'@'10.33.248.%' to database 'testDB'

    I have been giving the user access to the database with full privilages. But it still pops this errors up?

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Could not select database. No database selected issue

    Database names should be of the format zyry_myname , NOT myname . ie, testDB is not a valid name in the x10hosting set up.
    Nothing is always absolutely so.

  5. #5
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Could not select database. No database selected issue

    As far as I can tell from looking at your code, there's a lot of confusion over what a database is and what a table is. Even if everything goes perfectly, you'll be connected to a database named "zyry_nattlir" that has a table in it named "testDB" (not a great name for a table, if you think about it), then you're trying to run a SELECT against the database, not the table. Your page code uses the hash identifier "table" ($obj+>table = 'zyry_nattlir';) to refer to the database.

    My suggestion would be to get your naming conventions straightened out first so that you can tell the difference between a database and a table at a glance everywhere in your code. That will reduce the confusion to the point that it will become easy (or at least easier) to debug your problem.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread

Similar Threads

  1. No database selected
    By sahaquiel in forum Programming Help
    Replies: 5
    Last Post: 03-14-2010, 12:08 PM
  2. No database selected
    By sahaquiel in forum Ayuda Web
    Replies: 1
    Last Post: 03-13-2010, 09:32 AM
  3. No database selected?
    By mountainbux in forum Free Hosting
    Replies: 0
    Last Post: 05-14-2008, 07:22 PM
  4. No database selected...?
    By Mariodont in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 12-01-2007, 07:55 PM
  5. No database selected
    By SineC in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 08-30-2007, 04:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers