Results 1 to 3 of 3

Thread: OOPS - MySQL

  1. #1
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default OOPS - MySQL

    Hey guys,
    I am looking for help with my OOPS code for connecting to MySQL, just for people that don't know what OOPS is Object Oriented Programming.

    I am trying to connect to my database (MSQL) using this technology, however all my coding just does not seem to be working

    The first code I tried was this one, however since i am on a shared server it seems that mysqli does not work??? DONT ask me why.
    and as i am trying to make something for other site owners to use i need it to work with out them having to contact there administrators asking them to allow mysqli
    Code:
    $mysqli = new mysqli("localhost", "desvisa_v", "******", "desvisa_site");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    
    if ($result = $mysql->query("SELECT title FROM push_content LIMIT 10")) {
        printf("Select returned %d rows.\n", $result->num_rows);
    
        /* free result set */
        $result->close();
    }
    
    $mysqli->close();
    My second code, well i did not make the code, just copied and paste, BUT man it is way to confussing and i am just not understanding it.

    Code:
    class MyDatabase
    {
     // The var that stores the last 
     // used SQL statement
     var $SQLStatement = ""; 
     
     // The var that stores the error 
     // (if any)
     var $Error = "";
     
     function MyDatabase()
     {
      // Config for the database 
      // connection
      $this->DBUser = "desvisa_v";
      $this->DBPass = "*****";
      $this->DBName = "desvisa_site";
      $this->DBHost = "localhost";
     }
     
     function Connect()
     {
      //Connect to a mysql database
      $this->db = mysql_connect($this->DBHost, 
      $this->DBUser, $this->DBPass) or 
      die("MYSQL ERROR: ".mysql_error());
      // Select the database
      mysql_select_db($this->DBName, 
      $this->db) or die("MYSQL ERROR: 
      ".mysql_error());
     }
     
     // Disconnect from the MYSQL database
     function Disconnect()
     {
      mysql_close($this->db) or die("MYSQL 
      ERROR: ".mysql_error());
     } 
    }
    So if any one can come up with a OOPS way to connect to a mysql database, I would be forever in your debt.

    Thanks
    Russell
    The web in one word.

  2. #2
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Can anyone help?
    The web in one word.

  3. #3
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    This is an example similar to what I use on my web site and it works for me.
    I use a "switch" statement inside the function because I like to use a diferent
    database for each category.

    PHP Code:
    <?php
    class connections {
        public function 
    database($number$query){
            if(
    is_integer($number) && isset($query) && is_string($query)){
                switch(
    $number){
                    case 
    0:
                        
    $username "";    //used only at login
                        
    $password "";    //used only at login
                        
    $database "";
                        break;
                    case 
    1:
                        
    $database "";
                        break;
                    case 
    2:
                        
    $database "";
                        break;
                }
                if(!isset(
    $username) || !isset($password)){
                    
    $username "";    //regular user's
                    
    $password "";    //regular password
                
    }
                
    $db = @mysql_connect("localhost""xtiano5_".$username$password);
                
    $dbs = @mysql_select_db($database$db);
                
    $recordset = @mysql_query($query$db);
                if(
    is_resource($recordset)){
                    return 
    $recordset;
                }else{
                    
    mysql_close($db);
                    return;
                }
            }else{
                return;
            }
        }
    }
    ?>

    <?php
    require("file_where_class_is_located.php");
        
    $page = new connections( );
        
    $query "Your SQL query";
        
    $recordset $page -> database(0$query);
        if(
    mysql_num_rows($recordset) > 0){
            
    //your code here...
        
    }
    ?>
    You can always build the SQL query into this function so when you call the database, you can pass two arguments, 1) the database you want, which will include the adequate userids and passwords, and 2) the SQL query so upon success you'll get back a recordset instead of a simple connection resource. This should save you from having retype the same code on each function because if the "mysql_num_rows($recordset)" is greater than 0, then three things should have happened, the connection was established, the database was selected, and the query was performed and returned the desired results.

    Hope I didn't confuse you!
    Last edited by xtiano77; 08-21-2009 at 09:24 PM.

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
  •