Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: db class

  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    db class

    heres a class i wrote -- when i try $db->query(*) it returns the following:
    Code:
    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/trulyta/public_html/new/globals.inc.php on line 19
    the code:
    Code:
    class db {
       function open() {
           $this->host = "localhost";
           $this->name = "*";
           $this->user = "*";
           $this->pass = "*";
           $this->resource = mysql_connect($this->host, $this->user, $this->pass);
           mysql_select_db($this->name, $this->resource);
           register_shutdown_function($this->close);
       }
       function query($query) {
           // this is line 19, where it says the error is:
           $result = mysql_query($query, $this->resource);
           return $result;
       }
       function close() {
           mysql_close($this->resource);
       }
    }
    
    $db = new db;
    
    $db->open;
    $db->query("SELECT * FROM tt_drinks WHERE id = 1032");
    $db->close;
    any ideas would be greatly appreciated!!! thanks!

  • #2
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what you're doing is asking to execute the mysql query which you gave:
    query(*);


    -------------------
    i use this:
    its much easier:
    all i do is:
    //to run a query:
    $newqry = new query("Select * from bla bla bla....");

    to get the data from the database
    while($row = $newqry->putin_array()){
    echo $row['filed1'];
    }

    //to check how many rows affected:
    $newqry->affectedrows();

    //close the db
    $newqry->close();
    PHP Code:
    <?

    class query {
        var 
    $server;
        var 
    $db;
        var 
    $user;
        var 
    $password ;
        var 
    $error;
        var 
    $result;
        var 
    $conID
        function 
    query$sql )
        {
            
    $this->server "localhost";
            
    $this->db "";
            
    $this->user "";
            
    $this->password ""
            
    // connect to server and select database
            
    $db_con = @mysql_connect$this->server$this->user$this->password ) or die( $this->error "Database connection failure!" );
            
    $db_sel = @mysql_select_db$this->db ) or die ( $this->error mysql_error() . " Could not connect to specified database, db may not exist" );

            
    $this->conID $db_con
            
    // run query
            
    $this->result = @mysql_query$sql ) or die( $this->error "Error: " mysql_errno() . "&nbsp;<b>" mysql_error() . "</b>query:$sql" ); 
            
    // if errors then prints the error msg.
            
    if ( !$db_con ) {
                echo 
    $this->error;
                exit;
            } elseif ( !
    $db_sel ) {
                echo 
    $this->error ;
                exit;
            } 
        } 
        function 
    putin_array()
        {
            
    $sql_arrays mysql_fetch_array$this->result );
            return ( 
    $sql_arrays );
        } 
        function 
    numrows()
        {
            
    $sql_numrows mysql_num_rows$this->result );
            return 
    $sql_numrows;
        } 
        function 
    affectedrows()
        {
            
    $sql_affrows mysql_affected_rows();
            return 
    $sql_affrows;
        } 
        function 
    close()
        {
            @
    mysql_free_result$this->conID );
            @
    mysql_close$this->conID );
        } 


    ?>
    i made this myself,

    bye.
    Last edited by fimi; 06-01-2004 at 03:12 AM.
    |Tip: Use a template engine to separate your PHP code from HTML [Smarty]
    |Helpful: Follow the forum rules and posting guidelines, they apply to all of us [Rules] - [Posting Guidelines]
    |Me: [MSN][Y!][AIM][@] - fimi

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    $db->open;

    should be

    $db->open();

    there is no point closing the mysql connection unless you are using a persistant connection in the first place (which you probably don't need/want to do) , but even if you did ,
    register_shutdown_function($this->close);

    won't work (it actually closes the connection within the constructor not on shutdown) , not cos of the missing `()` , you need to call as so ..

    PHP Code:
    <?
    /*no brackets intentional this time*/
    register_shutdown_function(array(&$this'close'));
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help! Saved me hours of time!

    -J


  •  

    Posting Permissions

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