Results 1 to 2 of 2

Thread: mysql_select_db(): supplied argument is not a valid MySQL-Link resource

  1. #1
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default mysql_select_db(): supplied argument is not a valid MySQL-Link resource

    Hi all, very confused as to why this code does not function when a simple database connection works.

    This is the code that calls all the configuration.
    Code:
    // Load cfg
    require_once(DATA . 'config.php');
    
    // Load DB
    require_once(DATA . 'dbconfig.php');
    require_once(INC . 'mysql.php');
    $DB = new MySQL();
    $DB->server = DBHOST;
    $DB->user = DBUSER;
    $DB->password = DBPASS;
    $DB->database = DBNAME;
    $DB->connect();
    This is inside the dbconfig.php file
    Code:
    <?PHP
    define ("DBHOST", "localhost"); 
    define ("DBNAME", "dbase_dbase");
    define ("DBUSER", "dbase_user");
    define ("DBPASS", "==password");  
    ?>
    This is the mysql.php string.
    Code:
    <?php
    
    if(!defined('IN_MY'))
      die("Oops, I think you want to hack me!");
    
    
    // SQL Save function
    function SQLSave($theValue, $theType)
    {
    	$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    	
    	$theValue = function_exists("mysql_real_escape_string") ? 
    		mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    	
    	switch ($theType)
    	{
    		case "text":
    			$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    			break;
    		case "long":
    		case "int":
    			$theValue = ($theValue != "") ? intval($theValue) : "NULL";
    			break;
    		case "double":
    			$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    			break;
    		case "date":
    			$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    			break;
    		case "defined":
    			$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    			break;
    	}
    	return $theValue;
    }
    
    // ##################### MySQL Connection Handle Library #######################
    class MySQL 
    {
    	var $server   = "localhost";
    	var $user     = "root";
    	var $password = "";
    	var $database = "";
    
    	var $conn     = 0;
    	var $queryid  = 0;
    	var $row   = array();
    
    	var $errdesc  = "";
    	var $errno   = 0;
      
      	// ###################### connect #######################
      	function connect()
      	{
        	if(0 == $this->conn)
        	{
          		if($this->password=="")
          		{
            		$this->conn = mysql_connect($this->server.":3306",$this->user);
          		}
          		else
          		{
            		$this->conn = mysql_connect($this->server.":3306",$this->user,$this->password);
          		}
    
          		if(!$this->conn)
          		{
            		$this->error("Connection == false, connect failed");
          		}
    
          		if($this->database != "")
          		{
            		if(!mysql_select_db($this->database, $this->conn))
            		{
              			$this->error("cannot use database ".$this->database);
            		}
          		}
    
        	}
      	}
    The error spots are marked in red. For some reason it fails there and submits the fail error. Which is odd because when I use the simple database connector below, it always connects to the database.

    Code:
    // Load DB
    $link = mysql_connect('localhost', 'dbase_user', '==password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    
    $db_selected = mysql_select_db('dbase_dbase', $link);
    if (!$db_selected) {
    	die('<p>unable to locate the database</p>');
    	}
    Last edited by sniperman; 12-13-2010 at 09:46 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    A possible cause:
    somehow, your MySql class is not using your config file, and is falling back on its own default values:
    PHP Code:
        var $server   "localhost";
        var 
    $user     "root";
        var 
    $password "";
        var 
    $database ""
    ...which aren't valid, so the connection is denied.

    Try replacing those values (directly) in the class definition with your real database credentials, and see what happens. You can also try to print $DB->server;, etc., and see if the values are being successfully assigned.

    (And don't post your username/passwords on a public forum )
    Last edited by traq; 12-14-2010 at 04:27 AM. Reason: oversight/ clarification

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •