Class to Connect to the database and run a query.

Started on the old forum in May 2010 · view the original in the archive · old links redirect here

No replies · 906 views · started 16 years ago · 1 min read

TheNemesis Junior · 10 posts #1

Very basic database connect & run query class etc.

<?php

class Database {
    
    var $conn;
    var $select;
    
    function connect() {
        $this->conn = mysql_connect("localhost","root","") or die("Can't connect. " .mysql_error());
        $this->select = mysql_select_db ("ddl") or die("Can't select" .mysql_error());
        }
        
    function close_db(){
        if(isset($this->conn)){
           mysql_close($this->conn);
            unset($this->conn);
        }
    }
    
   public function query($sql){
        $this->query = mysql_query($sql);
   }
}

?>

Connect via external page:

$database = new Database;
$database->connect();

Connect and run query:

$database = new Database;
$database->connect();
$database->query("SELECT * FROM mytable");

Connect run query and close db:

$database = new Database;
$database->connect();
$database->query("SELECT * FROM mytable");
$database->close_db();

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close