communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "databases" - download "mySQL class"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

databases - mySQL class was created/last modified on Mon 15 Mar 2004 10:57 AM

a class with all often used mySQL functions
  
  <?php
  
class DB_Class {
  
   var
$db;
  
   function
DB_Class($dbname, $username, $password) {
    
$this->db = mysql_connect ('localhost', $username, $password)
      or die (
"Unable to connect to Database Server");
  
    
mysql_select_db ($dbname, $this->db)
      or die (
"Could not select database");
   }
  
   function
query($sql) {
    
$result = mysql_query ($sql, $this->db)
      or die (
"Invalid query: " . mysql_error());
    return
$result;
   }
  
   function
fetch($sql) {
    
$data = array();
    
$result = $this->query($sql);
    while(
$row = mysql_fetch_assoc($result)) {
      
$data[] = $row;
    }
    return
$data;
   }
  
   function
getone($sql) {
    
$result = $this->query($sql);
    if(
mysql_num_rows($result) == 0)
      
$value = FALSE;
    else
      
$value = mysql_result($result, 0);
    return
$value;
   }
  
  }
  
?>
  
  use like
  
  <?
  
  $dbconnect
= new DB_Class('table', 'user', 'password');
  
$query = "SELECT aaa FROM bbb WHERE $match = $search ORDER BY ccc DESC";
  
$result = $dbconnect->fetch($query);
  
  
?>

learn more about some of the PHP functions used in this snippet: array, connect, die, else, function, mysql, mysql_connect, mysql_error, mysql_fetch_assoc, mysql_num_rows, mysql_query, mysql_result, mysql_select_db, ord, return, var, while

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari