Skip to content

Instantly share code, notes, and snippets.

/uil

Created April 9, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8f21640d08d017a4d55ef832ad57d18e to your computer and use it in GitHub Desktop.
Save anonymous/8f21640d08d017a4d55ef832ad57d18e to your computer and use it in GitHub Desktop.
uil
<?php
# Драйвер работы с БД.
# Языковая настройка.
setlocale(LC_ALL, 'ru_RU.CP1251');
if ($config['database']['enabled'] == true)
{
$encoding = ($config['encoding'] == 'windows-1251') ? 'cp1251' : 'utf8';
# Подключение к БД.
mysql_connect($config['database']['mysql.zzz.com.ua'], $config['database']['zalypa74'], $config['database']['140212xxx']) or die('No connect with database');
mysql_query('SET NAMES ' . $encoding);
mysql_select_db($config['database']['wetal']) or die('No database');
mysql_query("ALTER TABLE `product` ADD `link2file` varchar(255) NOT NULL");
# Если нужно удалить существующие таблицы, раскомментируйте эти строки
#mysql_query("DROP TABLE `custom`");
#mysql_query("DROP TABLE `custom_item`");
# Установка jSale
$jsale_install = dirname(__FILE__) . '/../install.inc.php';
if (is_file($jsale_install))
include_once $jsale_install;
# Удаление файла установки
if (isset($jsale_installed))
unlink($jsale_install);
}
class MSQL
{
private static $instance; # ссылка на экземпляр класса
# Получение единственного экземпляра (одиночка)
public static function Instance()
{
if (self::$instance == null)
self::$instance = new MSQL();
return self::$instance;
}
# Выборка строк
# $query - полный текст SQL запроса
# результат - массив выбранных объектов
public function Select($query)
{
$result = mysql_query($query);
if (!$result)
die(mysql_error());
$n = mysql_num_rows($result);
$arr = array();
for($i = 0; $i < $n; $i++)
{
$row = mysql_fetch_assoc($result);
$arr[] = $row;
}
return $arr;
}
# Вставка строки
# $table - имя таблицы
# $object - ассоциативный массив с парами вида "имя столбца - значение"
# результат - идентификатор новой строки
public function Insert($table, $object)
{
$columns = array();
$values = array();
foreach ($object as $key => $value)
{
$key = mysql_real_escape_string($key . '');
$columns[] = $key;
if ($value === null)
{
$values[] = 'NULL';
}
else
{
$value = mysql_real_escape_string($value . '');
$values[] = "'$value'";
}
}
$columns_s = implode(',', $columns);
$values_s = implode(',', $values);
$query = "INSERT INTO `$table` ($columns_s) VALUES ($values_s)";
$result = mysql_query($query);
if (!$result)
die(mysql_error());
return mysql_insert_id();
}
# Изменение строк
# $table - имя таблицы
# $object - ассоциативный массив с парами вида "имя столбца - значение"
# $where - условие (часть SQL запроса)
# результат - число измененных строк
public function Update($table, $object, $where)
{
$sets = array();
foreach ($object as $key => $value)
{
$key = mysql_real_escape_string($key . '');
if ($value === null)
{
$sets[] = "$key=NULL";
}
else
{
$value = mysql_real_escape_string($value . '');
$sets[] = "$key='$value'";
}
}
$sets_s = implode(',', $sets);
$query = "UPDATE `$table` SET $sets_s WHERE $where";
$result = mysql_query($query);
if (!$result)
die(mysql_error());
return mysql_affected_rows();
}
# Удаление строк
# $table - имя таблицы
# $where - условие (часть SQL запроса)
# результат - число удаленных строк
public function Delete($table, $where)
{
$query = "DELETE FROM `$table` WHERE $where";
$result = mysql_query($query);
if (!$result)
die(mysql_error());
return mysql_affected_rows();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment