SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot asrasrasr's Avatar
    Join Date
    Apr 2009
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error with pear database connection

    Hello friends,

    I am using php pear connectivity. I have following DB.php file.

    Code PHP:
    <?php
    	class DB
    	{
     
    		var $handle;
    		var $host;
    		var $port;
    		var $name;
    		var $username;
    		var $password;
     
    		/*
    			Constructor
    	      - Initialises class variables
    		*/
    		function __construct($host, $port, $name, $username, $password)
    		{
    			$this->host = $host;
    			$this->port = $port;
    			$this->name = $name;
    			$this->username = $username;
    			$this->password = $password;
    		}
     
    		/*
    			Destructor
    			Cleans up database connection!
    		*/
    		function __destruct()
    		{
    			if($this->handle) 
    				mysql_close($this->handle);
    		}
     
    		/*
    			connectDatabase()
    			 - Connects to mysql and selects a database.
    			 - returns connection handle on success, redirects on failure
    			 - @ surpresses errors otherwise setting headers fails
    		*/
    		function connectDatabase()
    		{ 
    			if($this->handle) return $this->handle;
     
    			$this->handle = @mysql_connect($this->host, $this->username, $this->password, true);
     
    			if(@mysql_select_db($this->name, $this->handle))
    			{
    				return $this->handle;
    			}
    			else
    			{
    				return false;
    			}
    		}
     
    		/*
    		 * query
    		 * Found on PHP comments
    		 * Used with slight modifications
    		 * 
    		 */
    		function query($query)
    		{
       	 		$r = @mysql_query($query, $this->handle);
        		if(mysql_errno()) return false;
     
        		if(strtolower(substr($query, 0, 6)) != 'select' ) 
        			return array(mysql_affected_rows(), mysql_insert_id());
     
        		$count = @mysql_num_rows($r);	
        		if(!$count) return 0;
     
        		if($count == 1)
        		{
            		$f = mysql_fetch_array($r);
            		mysql_free_result($r);
     
            		if(count($f) == 1)
            		{
                		list($key) = array_keys($f);   
                		return $f[$key];
            		} 
            		else
            		{
                		$all = array();
                		$all[] = $f;
                		return $all;
            		}
        		} 
        		else 
        		{
            		$all = array();
            		for( $i = 0; $i < $count; $i++ )
            		{
                		$f = mysql_fetch_array($r);
                		$all[] = $f;
            		}
            	}
     
            	mysql_free_result($r);
            	return $all;
        		}
    		}
    ?>

    and access this code with following index.php file

    Code PHP:
    <html>
    <body>
    <?php
    require_once("DB.php");
    $db_host = "localhost";
    $dbn = "money4_m4sdat";
    $db_user = "root";
    $db_password = ""; 
    $db_type = "mysql";
     
    //$db =& DB::Connect( 'mysql://root@localhost/money4_m4sdat', array() );
    $db = DB::Connect("$db_type://$db_user:$db_password@$db_host/$dbn"); 
    if (PEAR::isError($db)) { die($db->getMessage()); }
     
    $res = $db->query( 'SELECT * FROM movies' );
    while( $res->fetchInto( $row ) )
    {
    ?>
    <a href="rate.php?id=<?php echo($row[0]) ?>"><?php echo($row[1]) ?></a><br/>
    <?php
    }
    ?>
    </body>
    </html>

    Bit this return me following fatal error.
    Fatal error: Call to undefined method DB::Connect() in D:\xampp\htdocs\m4s\comments\index.php on line 12


    Where i a wrong. can anyone help me plaese.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Your DB object is not pear at all, neither does it have a static method named connect.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot asrasrasr's Avatar
    Join Date
    Apr 2009
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Than how i make this pear object plesae give me a breif script.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Head on over to the PEAR documentation, if you have any problems just post.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Zealot asrasrasr's Avatar
    Join Date
    Apr 2009
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am unable to understand wht i do?


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
  •