Results 1 to 5 of 5

Thread: OOP error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    150

    OOP error

    Hi Folks,

    I am trying to use a class for my database log in details in the OOP way, but when I instantiate the class in my work, I get an error message that 'No database was selected'. My code are pasted below and I thought that I have selected a database already, or am I doing something terribly wrong somewhere? I will appreciate your help please.

    The connection class code
    Code:
    <?php 
    class Connect {
    	                public $mysql;
    	                private $host       = 'localhost';
    			private $username   = 'root';
    			private $pwd        = '';
    			private $database   = 'students';
    			
    			function __construct(){
    			$this->mysql = new mysqli($this->host, $this->username,$this->pwd,$this->database)
    			 or die(mysql_error());
    				echo "Successfully connected";	
    				}	
    				
    			function __destruct(){
    				//close mysql database connection
    			  $this->mysql->close();
    				}
    	        }
       ?>
    The test code file has the code below.
    Code:
    <?php 
    include ('conn_class.php');
    
    $Connect = new Connect();
    ?>
    
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Student Zone</title>
    <style type="text/css">
    body {font-family: Arial, Helvetica, sans-serif;}
    #wrapper {width: 550px; 
              height: auto; 
    		  border: 1px solid #ccc; margin: 0px auto;}
    #toparea {height: 70px; 
                 border-bottom: 1px solid #ccc; 
    			 font-size: 250%; 
    			 padding-left: 10px;
    			 padding-top: 30px;}
    			 
    #content {height: auto;}
    #content p{margin: 0px; padding-left: 20px; padding-top: 15px;}
    			 
    #bottom {clear: both; 
                 border-top: 1px solid #ccc;
    			 height: 25px;
    			 text-align: center;
    			 font-size: 12px;}
    </style>
    </head>
    
    <body>
    <div id="wrapper">
    <div id="toparea">Student Zone</div>
    <div id="centent"><p>Search a course below.</p>
    <?php 
    $select = mysql_query("SELECT course_name FROM courses")
      or die(mysql_error());
      while ($row = mysql_fetch_array($select)) {
    	  echo $row['course_name'];
    	  }
    
    ?>
    
    </div>
    <div id="bottom"><?php echo "&copy;" . date('Y'); ?></div>
    </div>
    
    </body>
    </html>
    I will appreciate some help please.

    Thanks,
    Menre

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: OOP error

    In your code, you are using mysql_error() function, that is you are mixing up the legacy mysql function with mysqli. When creating an object of mysqli, it would return an object.

    Check this example: http://www.php.net/manual/en/mysqli....p#example-1625


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    150

    Re: OOP error

    Thanks for your response. I tried following your advice and doing it the way they suggested, but got the same error message 'No database selected'. I even tried copying and modifying their code to the one below and the error message was the same. But when I use the procedural style to connect, everything works fine.

    Code:
    <?php
    
    class Connect extends mysqli {
        public function __construct($localhost, $root, $pass, $db) {
            parent::__construct($localhost, $root, $pass, $db);
    
            if (mysqli_connect_error()) {
                die('Connect Error (' . mysqli_connect_errno() . ') '
                        . mysqli_connect_error());
            }
        }
    }
    
    $db = new Connect('localhost', 'root', '', 'students');
    
    echo 'Success... ' . $db->host_info . "\n";
    
    $db->close();
    ?>
    Or am I still doing something wrong?

    Thanks,
    Menre

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: OOP error

    When you call mysql_connect and mysql_select_db, the connection is persisted for all mysql_* functions.
    When you create a mysqli object, the connection belongs to that object only.

    So this is wrong:
    PHP Code:
    $db = new mysqli(...);
    mysql_query(......) 
    This is right:
    PHP Code:
    $db = new mysqli(...);
    $db->query(.......) 
    The class you've created doesn't really add any value (unless you've omitted most of it for the sake of this post). It actually makes your code more cumbersome because you've hidden the mysqli object within a class without exposing any methods for actually querying the database.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    150

    Re: OOP error

    I agree with you that the class doesn't actually add any value to the code. I just prefer a simple way of making the MySQL database connection.

Posting Permissions

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



Click Here to Expand Forum to Full Width