The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Choosing DB.....why cant i? [moved]

Discussion in 'Database Discussions' started by russjman, Sep 21, 2005.

  1. russjman

    russjman Member

    Joined:
    Sep 20, 2005
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Choosing DB.....why cant i?

    :confused:
    So i posted this issue yesterday, and for some reason the thread doesn't exist today.

    I cant choose my mysql DB. I appearantly connect, i use the code that cpanel generates, and thats fine. when i then go to select my DB it says:

    No Database Selected


    Code:
    $dbh=mysql_connect ($localhost, $dbname, $password) 
    	or die ('I cannot connect to the database because: ' . mysql_error());
    	
    	if(!$dbh){
    		echo "this REALLY didnt connnect, and was lying to you!";
    		
    	}
    		
    	if(!mysql_selectdb($dbname))
    	{
    
    		$emsg .= mysql_error();
    		echo "Cannot select DB because.......$emsg";
    
    	}
    ive reset the user and the privalages numerous times and still nothing

    if its not obvious by now, i am infact a noob when it comes to this, but getting stuck on something like this is rediculous.


    Someone before happened to mention that using Bugzilla might help. Never heard of it, nor do i know that its for, even after going to the site. A little help on that would be nice too.
     
  2. russjman

    russjman Member

    Joined:
    Sep 20, 2005
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    At this point i welcome insults and teasing as a response. Give something .......anything.......i need someone to acknowledge my exixtance.


    oh.......and having threads moved.......wondering why they disappeared.........is ANNOYING as hell!!!
     
  3. webignition

    webignition Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    1,880
    Likes Received:
    0
    Trophy Points:
    36
    When you run the code you mention, what is the output?

    if it is not working, it should be generating an error message. What error do you get?
     
  4. russjman

    russjman Member

    Joined:
    Sep 20, 2005
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    praise the LORD!!


    "Cannot select DB because......No Database Selected"

    which comes from where i try to choose the DB. A quandry of sorts....hmm



    thx man
     
  5. webignition

    webignition Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    1,880
    Likes Received:
    0
    Trophy Points:
    36
    In which case double check the value you are specifying for $dbname - make sure that it is a valid database to which you have access.
     
  6. russjman

    russjman Member

    Joined:
    Sep 20, 2005
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    dbname is the the name of the DB and the user. Both were the same to try to avoid issues like this.

    for the user name, when i defined it in cpanel, it automatically added my user name to the front. So the user name is rusman_top10, and i defined the db by the same name. I have wordpress running fine in the same subdomain. And the code to connect and choose a DB is the same as what i have.

    Is there some sort of update that i can do on cpanel. As far as i know, ive never updated anything, but i hear talk of it all over the place 'round here.
     
  7. jester.ro

    jester.ro Well-Known Member
    PartnerNOC

    Joined:
    Feb 6, 2004
    Messages:
    304
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Bucharest, Romania
    cPanel Access Level:
    DataCenter Provider
    what's the '$' sign doing before localhost in your connect code?

    have you go somewhere $localhost defined as 'localhost' or...?
     
  8. Murtaza_t

    Murtaza_t Well-Known Member

    Joined:
    Jan 24, 2005
    Messages:
    476
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Earth
    cPanel Access Level:
    Website Owner
    Check your config.php file and check if the DB name is specified correctly (as user_dbname) and so the users & password.

    Also there is point on what jester.ro said. Have you declared $localhost as localhost in your config.php?
     
  9. ckh

    ckh Well-Known Member

    Joined:
    Dec 6, 2003
    Messages:
    356
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Phoenix, AZ
    cPanel Access Level:
    DataCenter Provider
    Code:
    $dbh=mysql_connect ($localhost, $dbname, $password) 
    	or die ('I cannot connect to the database because: ' . mysql_error());
    	
    	if(!$dbh){
    		echo "this REALLY didnt connnect, and was lying to you!";
    		
    	}
    		
    	if(!mysql_selectdb($dbname))
    	{
    
    		$emsg .= mysql_error();
    		echo "Cannot select DB because.......$emsg";
    
    	}
    Don't you need:

    mysql_select_db("$dbname")

    Try:

    Code:
    $dbh=mysql_connect ($localhost, $dbname, $password);
    	mysql_select_db("$dbname")
    	or die ('I cannot connect to the database because: ' . mysql_error());
    	
    	if(!$dbh){
    		echo "this REALLY didnt connnect, and was lying to you!";
    		
    	}
    		
    	if(!mysql_selectdb($dbname))
    	{
    
    		$emsg .= mysql_error();
    		echo "Cannot select DB because.......$emsg";
    
    	}
     
  10. jester.ro

    jester.ro Well-Known Member
    PartnerNOC

    Joined:
    Feb 6, 2004
    Messages:
    304
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Bucharest, Romania
    cPanel Access Level:
    DataCenter Provider
    haha, good one chk

    we all missed that one.


    but you forgot something, the code should look like:



    first, a mysql_connect statement

    mysql_connect($host, $username, $password)

    then a mysql_select_db

    mysql_select_db($dbname);

    so, the initial code had 2 mistakes:
    1. it was missing the username
    2. it was missing a select_db statement.
     
  11. russjman

    russjman Member

    Joined:
    Sep 20, 2005
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    woo hoo

    ya i declaired all the variables before using them as part of a class structure at the top. As far is putting quotes on the $dbname.....is it nessesary if its already defined as a string above?

    mf'er it worked! :D

    THANK YOU SOOO MUCH.........ALL of you!!!

    I defined $dbname, $localhost, $password as part of a class structure when it is initialized
    then i call the function to $dbobject.connect 2db() from another file. Some how the those vars were not defined when i defined $dbobject when i called connect2db(). It worked when i defined them within the the actual function connect2db().

    part of another problem that i fixed, but doesn't make sense is:
    Code:
            $SQL = "SHOW tables";
    	$data=mysql_query($SQL, $dbh);
    	
    	if (!$data){
    			//$emsg = "Data not loading from user table";
    			$emsg .= mysql_error();
    			echo $emsg;
    		}
    this only works when i DONT give mysql_query $dbh. WHY? and does it really matter if i query without giving it? It just dawned on me that its the same as the problem with the other variables for connecting. Am I somehow not defining them globally?

    ok....heres the whole script for dbobject:
    Code:
    <?php
    
    class db_object{
    	
    	var $localhost = "localhost";
    	var $user = "rusman_top10";
    	var $dbname = "rusman_top10";
    	var $password = "top10";					
    	var $dbh;									//db handle
    	
    		
    }
    
    
    
    /*update information in the data base
    $id - id key in the table
    $tablename - name of the table being updated
    
    
    */
    
    
    
    
    function updateDB($table, $id, $new_info){
    	
    	
    	
    }
    
    		
    function get_table(){
    	
    		
    	$SQL = "SHOW tables";
    	$data=mysql_query($SQL, $dbh);
    	
    	if (!$data){
    			//$emsg = "Data not loading from user table";
    			$emsg .= mysql_error();
    			echo 'I cannot define data because: ' . mysql_error();
    		}
    			
    	
    		
    	$rownum=mysql_num_rows($data);
    	echo $rownum;
    	
    	
    	
    }
    
    
    function connect2db(){
    	
    	
    	$localhost = "localhost";
    	$user = "rusman_top10";
    	$dbname = "rusman_top10";
    	$password = "top10";					
    	
    	
    	
    	
    	echo $localhost."<br>";
    	echo $dbname."<br>";
    	echo $password."<br>";
    	
    	$dbh=mysql_connect ($localhost, $dbname, $password);
    	mysql_select_db("$dbname")
    	or die ('I cannot connect to the database because: ' . mysql_error());
    	
    	if(!$dbh){
    		echo "this REALLY didnt connnect, and was lying to you!";
    		
    	}
    		
    
    }
    
    
    ?>
     
  12. ckh

    ckh Well-Known Member

    Joined:
    Dec 6, 2003
    Messages:
    356
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Phoenix, AZ
    cPanel Access Level:
    DataCenter Provider

    He stated that the dbname and the username was the same to avoid confusion. So, he'd be able to use $dbname as both the name of the database and the name of the user.

    Anyway, as long as it's working....

    Chris
     
Loading...

Share This Page