Open Sencillo  2014.008
 All Data Structures Files Functions Variables Pages
core_sql.php
Go to the documentation of this file.
1 <?PHP
2 /*~ core_sql.php
3 .---------------------------------------------------------------------------.
4 | Software: Sencillo Core |
5 | Version: 2014.008 |
6 | Contact: ph@mastery.sk |
7 | ------------------------------------------------------------------------- |
8 | Author: Bc. Peter Horváth (original founder) |
9 | Copyright (c) 2014, Bc. Peter Horváth. All Rights Reserved. |
10 | ------------------------------------------------------------------------- |
11 | License: Distributed under the General Public License (GPL) |
12 | http://www.gnu.org/copyleft/gpl.html |
13 | This program is distributed in the hope that it will be useful - WITHOUT |
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
15 | FITNESS FOR A PARTICULAR PURPOSE. |
16 '---------------------------------------------------------------------------'
17 ~*/
27 class mysql
28 {
29  public $DBHost;
30  public $DBName;
31  public $DBUser;
32  public $DBPass;
33  private $checksum;
34  public $con;
35 
44  {
45  $this->DBHost = $DBHost;
46  $this->DBName = $DBName;
47  $this->DBUser = $DBUser;
48  $this->DBPass = $DBPass;
49 
50  if(($this->DBHost!='')&&($this->DBUser!='')&&($this->DBPass!='')&&($this->DBName!=''))
51  {
52  $this->checksum=md5($this->DBHost.$this->DBUser.$this->DBPass.$this->DBName);
53  }
54  $this->con = mysql_connect($this->DBHost, $this->DBUser, $this->DBPass);
55  if(! $this->con)
56  {
57  die("<b>core_sql: MySQL connection failed!</b> ".mysql_error());
58  }
59  mysql_select_db($this->DBName, $this->con);
60  }
61 
66  final public function query($sql)
67  {
68  mysql_query($sql);
69  }
70 
75  final public function write($sql)
76  {
77  $this->query($sql);
78  }
79 
83  final public function close()
84  {
85  mysql_close($this->con);
86  }
87 
92  final public function test()
93  {
94  if($this->checksum==md5($this->DBHost.$this->DBUser.$this->DBPass.$this->DBName))
95  {
96  if(! $this->con)
97  {
98  return mysql_error();
99  }
100  else
101  {
102  return true;
103  }
104  }
105  }
106 }
107 
117 class mysqlEdit extends mysql
118 {
119  private $construct;
120  private $table;
121  private $sql;
122  private $result;
123  private $column;
124  private $setupdate;
125  private $colout;
126  private $out;
127  private $metaout;
128  private $csum;
129  private $sizeout;
130 
136  public function newColumn($name,$type="INT")
137  {
138  $this->construct .= ' , `'.$name.'` '.strtoupper($type).'';
139  }
140 
145  public function createTable($name)
146  {
147  $this->table = $name;
148  $this->query('CREATE TABLE IF NOT EXISTS `'.$name.'` ( `id` INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(`id`)'.$this->construct.');');
149  }
150 
155  public function openTable($name)
156  {
157  $this->table = $name;
158  $this->sql="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='".$this->DBName."' AND `TABLE_NAME`='".$this->table."';";
159  $this->con=mysql_connect($this->DBHost,$this->DBUser,$this->DBPass);
160  mysql_select_db($this->DBName, $this->con);
161  $this->result=mysql_query($this->sql);
162  while($row=mysql_fetch_array($this->result))
163  {
164  $this->column.='`'.$row['COLUMN_NAME'].'`,';
165  }
166  }
167 
173  public function insert($values)
174  {
175  $this->query('INSERT INTO '.$this->table.' ('.substr($this->column, 0, -1).') VALUES (null,'.$values.');');
176  }
177 
183  public function set($column,$value)
184  {
185  if(is_numeric($value))
186  {
187  $this->setupdate.='`'.$column.'`='.$value.',';
188  }
189  else
190  {
191  $this->setupdate.='`'.$column.'`="'.$value.'",';
192  }
193  }
194 
201  public function update($if,$sets=null)
202  {
203  $this->query('UPDATE '.$this->table.' SET '.substr($this->setupdate, 0, -1).$sets.' WHERE '.$if.';');
204  }
205 
212  public function delete($if)
213  {
214  if($if=="all")
215  {
216  $this->query('DELETE FROM `'.$this->table.'` WHERE `id`>0;');
217  }
218  else
219  {
220  $this->query('DELETE FROM `'.$this->table.'` WHERE '.$if.';');
221  }
222  }
223 
231  public function output($if="`id`>0",$order="`id` ASC",$limit=1000)
232  {
233  $this->sql="SELECT * FROM `".$this->table."` WHERE ".$if." ORDER BY ".$order." LIMIT ".$limit.";";
234  $this->con=mysql_connect($this->DBHost,$this->DBUser,$this->DBPass);
235  mysql_select_db($this->DBName, $this->con);
236  $this->result=mysql_query($this->sql);
237  $this->colout=explode(",",str_replace("`","",substr($this->column, 0, -1)));
238  $i=0;
239  $j=0;
240  $this->out = array('header'=>$this->colout,'line'=>array(array()));
241  $this->csum = md5($this->con);
242  while($row=mysql_fetch_array($this->result))
243  {
244  $i=0;
245  $j++;
246  while(sizeof($this->colout)>$i)
247  {
248  $this->out['line'][$j][$i]=$row[$this->colout[$i++]];
249  }
250  }
251  return $this->out;
252  }
253 }
254 $mysql = new mysqlEdit($DBHost,$DBName,$DBUser,$DBPass);
255 ?>
update($if, $sets=null)
Definition: core_sql.php:201
$DBName
Definition: core_sql.php:30
test()
Definition: core_sql.php:92
newColumn($name, $type="INT")
Definition: core_sql.php:136
createTable($name)
Definition: core_sql.php:145
__construct($DBHost, $DBName, $DBUser, $DBPass)
Definition: core_sql.php:43
$DBUser
Definition: core_sql.php:31
query($sql)
Definition: core_sql.php:66
$DBPass
Definition: core_sql.php:32
$sql
$mysql
Definition: core_sql.php:254
write($sql)
Definition: core_sql.php:75
output($if="`id`>0", $order="`id` ASC", $limit=1000)
Definition: core_sql.php:231
openTable($name)
Definition: core_sql.php:155
close()
Definition: core_sql.php:83
set($column, $value)
Definition: core_sql.php:183
insert($values)
Definition: core_sql.php:173
$DBHost
Definition: core_sql.php:29