manifest139
-
Content Count
2 -
Joined
-
Last visited
Posts posted by manifest139
-
-
May be it helps for an MySQL entry
</P><P><?php class Db_Default{   // connection  var $db_user = 'username';  var $db_pass = 'password';  var $db_db = 'database_name';  var $db_host = 'localhost';   var $connection;   // table names (examples)  // --one user has many needs--  var $tbl_user = 'users';  var $tbl_user_needs = 'user_needs';   function Db_Default(){  $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);  mysql_select_db($this->db_db);  }   function query($sql){  $result = mysql_query($sql, $this->connection);  return $result;  }   function get_all($sql){  $result = $this->query($sql);  $ar = array();  while($row = mysql_fetch_assoc($result)){$ar[] = $row;}  return count($ar) ? $ar : null;  }   function get_row($sql){  $result = $this->query($sql);  $row = mysql_fetch_assoc($result);  return count($row) ? $row : null;  }   function get_one($sql){  $result = $this->query($sql);  $row = mysql_fetch_array($result);  return isset($row[0]) ? $row[0] : null;  }  } ?></P><P>
Examples<?php // example // select all users $db = new Db_Default(); $all_users = $db->get_all("SELECT * FROM {$db->tbl_users} ORDER BY {$db->tbl_users}.name"); // next example with left join // get all needs from one user $id = (int)$_POST['id_user']; $q = "SELECT {$db->tbl_users}.name as username,  {$db->tbl_user_needs}.hardware,  {$db->tbl_user_needs}.software    FROM {$db->tbl_users}    LEFT JOIN {$db->tbl_user_needs}  ON {$db->tbl_users}.id = {$db->tbl_user_needs}.id_users    WHERE {$db->tbl_users}.id = '$id';"; $all_users_needs = $db->get_all($q); // next example // check if user exists with get_one() $id = (int)$_POST['id_user']; $q = "SELECT COUNT(*) >= 1 FROM {$db->tbl_users} WHERE id = '$id' LIMIT 1;"; $user_exists = (bool)$db->get_one($q); // next example // select one single row from user $id = (int)$_POST['id_user']; $q = "SELECT {$db->tbl_users}.* FROM {$db->tbl_users} WHERE id '$id' LIMIT 1;"; $user_data = $db->get_row($q); ?>
A Very Small Db Class For Beginner
in Programming
Posted · Report reply
May be you (as Pro.) can help to optimize a bit this query?
It is a query from this Project:
https://sourceforge.net/projects/openrentstock/