mysql til mysqli
HejEn af min venner spørg mig om jeg vil hjælp ham videre med sin side da han kun mangler den her kode så er han færdig med mysqli. han har selv bare resten af siden kode til mysqli, men han har love mange problemer med det her så han spørger mig om jeg havde lyst men dog er jeg ikke 100% good til mysqli og php så jeg vil høre om i kun hjælp min ven videre men det vil også betyde meget for mig hvis i har lyst til det her!
<?php
define('SB_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/test/');
class Shoutbox
{
private $_options = array(
'db_server' => 'localhost',
'db_charset' => 'utf8',
'db_user' => 'xxx',
'db_password' => 'xxx',
'db_name' => 'xxx',
'db_prefix' => 'chat_',
'charset' => 'utf-8',
'heading' => 'xxx',
'refresh' => 1.0,
'spam_protect' => 1,
'enable_bbcode' => true,
'enable_smilies' => true
);
private $_db = null;
public function __construct($options = null)
{
if (!is_null($options)) {
$this->_options = array_merge($this->_options, $options);
}
$this->disable_magic_quotes();
$this->connect_database();
}
private function connect_database()
{
$this->_db = @mysql_connect($this->_options['db_server'], $this->_options['db_user'], $this->_options['db_password'], false);
if ($this->_db === false) {
throw new Exception('Unable to connect to the database, please check your configuration.');
}
if (@mysql_select_db($this->_options['db_name'], $this->_db) === false) {
throw new Exception('Unable to select the database, please check your configuration.');
}
mysql_query('SET NAMES "' . $this->_options['db_charset'] . '"', $this->_db);
}
private function disable_magic_quotes()
{
if (get_magic_quotes_gpc()) {
$_GET = $this->remove_magic_quotes($_GET);
$_POST = $this->remove_magic_quotes($_POST);
$_COOKIE = $this->remove_magic_quotes($_COOKIE);
@ini_set('magic_quotes_gpc', 0);
}
if (get_magic_quotes_runtime()) {
set_magic_quotes_runtime(false);
}
}
private function remove_magic_quotes($data)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->remove_magic_quotes($value);
}
else {
$data[$key] = stripslashes($value);
}
}
return $data;
}
public function db_insert($rows, $data)
{
$mysql_row = '';
foreach ($rows as $row) {
$mysql_row .= '`' . $row . '`, ';
}
$mysql_row = mb_substr($mysql_row, 0, -2, $this->_options['charset']);
$mysql_data = '';
foreach ($data as $value) {
$mysql_data .= '"' . $value . '", ';
}
$mysql_data = mb_substr($mysql_data, 0, -2, $this->_options['charset']);
mysql_query(
'INSERT INTO `' . $this->_options['db_prefix'] . 'shouts` (' . $mysql_row . ') VALUES (' . $mysql_data . ')',
$this->_db
);
}
public function db_get_messages($last_id)
{
settype($last_id, 'integer');
if (empty($last_id)) {
$result = mysql_query('SELECT COUNT(`id`)-10 FROM `' . $this->_options['db_prefix'] . 'shouts`');
$last_id = mysql_result($result, 0); // FEHLER
}
$mysql_string = '';
$result = mysql_query(
'SELECT `id`, `name`, `message`, `time` FROM `' . $this->_options['db_prefix'] . 'shouts` WHERE `id` > "' . (int) $last_id . '" ORDER BY `id` ASC',
$this->_db
);
while ($row = mysql_fetch_object($result)) {
if ($row->name == 'Bot') {
$mysql_string .= '.append(\'<div id="' . $row->id . '" class="bot"><span class="time" title="' . date('d.m.Y - H:i:s', $row->time) . '">(' . date('H:i', $row->time) . ')</span> ' .
'<span class="name">' . $this->clean_input($row->name) . '</span>: <span class="message">' . $this->deal_output($row->message) . '</span></div>\')';
}
else {
$mysql_string .= '.append(\'<div id="' . $row->id . '"><span class="time" title="' . date('d.m.Y - H:i:s', $row->time) . '">(' . date('H:i', $row->time) . ')</span> ' .
'<span class="name">' . $this->clean_input($row->name) . '</span>: <span class="message">' . $this->deal_output($row->message) . '</span></div>\')';
}
}
mysql_free_result($result);
return $mysql_string;
}
public function deal_output($data)
{
return $this->replace_smilies($this->convert_linebreaks($this->set_wordwrap($this->bbcode($this->clean_input($data)))));
}
public function db_clean($data)
{
return mysql_real_escape_string($data, $this->_db);
}
public function clean_input($data)
{
return trim(htmlspecialchars(mb_convert_encoding($data, $this->_options['charset'], mb_detect_encoding($data)), ENT_QUOTES, $this->_options['charset'], true));
}
public function bbcode($data)
{
if ($this->_options['enable_bbcode']) {
// Replace
$data = preg_replace('/\[b\](.*?)\[\/b\]/', '<b>$1</b>', $data);
// Replace
$data = preg_replace('/\[i\](.*?)\[\/i\]/', '<i>$1</i>', $data);
// Replace
$data = preg_replace('/\+).*\](.*)\[\/url\]/', '<a href="$1" target="_blank" rel="nofollow">$2</a>', $data);
// Replace [url]
$data = preg_replace('/\[url\](.*)\[\/url\]/', '<a href="$1" target="_blank" rel="nofollow">$1</a>', $data);
}
return $data;
}
private function replace_smilies($text)
{
if ($this->_options['enable_smilies']) {
$image_tag = array('<img src="media/smilies/', '" alt="" />');
$smilies = array(
':-)' => $image_tag[0] . '1.png' . $image_tag[1],
';-)' => $image_tag[0] . '2.png' . $image_tag[1],
':)' => $image_tag[0] . '1.png' . $image_tag[1],
';)' => $image_tag[0] . '2.png' . $image_tag[1],
':D' => $image_tag[0] . '3.png' . $image_tag[1],
':-D' => $image_tag[0] . '3.png' . $image_tag[1],
':P' => $image_tag[0] . '4.png' . $image_tag[1],
';P' => $image_tag[0] . '5.png' . $image_tag[1],
';D' => $image_tag[0] . '6.png' . $image_tag[1],
':(' => $image_tag[0] . '31.png' . $image_tag[1],
':-(' => $image_tag[0] . '31.png' . $image_tag[1],
'=)' => $image_tag[0] . '7.png' . $image_tag[1],
'=D' => $image_tag[0] . '3.png' . $image_tag[1],
'=P' => $image_tag[0] . '4.png' . $image_tag[1],
'xD' => $image_tag[0] . '8.png' . $image_tag[1],
':S' => $image_tag[0] . '17.png' . $image_tag[1],
'xS' => $image_tag[0] . '29.png' . $image_tag[1],
':O' => $image_tag[0] . '14.png' . $image_tag[1],
'=O' => $image_tag[0] . '14.png' . $image_tag[1],
'=/' => $image_tag[0] . '20.png' . $image_tag[1],
'=]' => $image_tag[0] . '21.png' . $image_tag[1],
'$_$' => $image_tag[0] . '26.png' . $image_tag[1],
'o_O' => $image_tag[0] . '28.png' . $image_tag[1]
);
$text = str_ireplace(array_keys($smilies), array_values($smilies), $text);
}
return $text;
}
private function convert_linebreaks($text)
{
$text = str_replace("\n", '', nl2br($text));
return $text;
}
private function set_wordwrap($text)
{
return wordwrap($text, 88, "\n", true);
}
public function get_heading()
{
return $this->_options['heading'];
}
public function get_charset()
{
return $this->_options['charset'];
}
public function get_refresh_rate()
{
return (float) $this->_options['refresh'];
}
public function get_spam_protection_rate()
{
return (int) $this->_options['spam_protect'];
}
public function is_ajax()
{
return ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') ? true : false;
}
protected function __clone()
{}
}
?>
det vil være super hvis du gad at hjælp min ven til at han fik klart det om til mysqli frem for mysql.
Jeg tør ikke at lege med 200 linje kode da jeg ikke kan overhoved 70% kode og funktioner af koden.
Det vil betyde meget for mig og min ven hvis du vil hjælp ham videre dog ha han selv bygge siden om fra mysql til mysqli, dog er det her bare hans side kode. men jeg love ham at kigge på den men jeg vil ikke kast mig ud i det overhovedet!