[cpo+] : Modifikationen

« zurück

mySQLi

Kategorie:PHP-Upgrade
Beschreibung:Das PHP-Feature mySQL wird durch mySQLi ersetzt.
Erstellt von:Markus Rösel
Geprüft von:
Veröffentlicht von:
CPO+ Version:4.1.6+1
Kurze Erläuterung der Codeanpassung:Die mySQL-Funktionen werden durch ihre prozeduralen mySQLi-Pendants ersetzt.
Dabei ist auf die Angabe der Link-ID zu achten.

Code vorher:

[[ ********* File: _setup/step2.php ********* ]] if ($ok && !@mysql_connect($_POST['DB_hostname'], $_POST['DB_login'], $_POST['DB_password'])) { $message[] = ___('Unable to connect to the database. Please check database hostname, login and password.'); $errors['connect'] = ___('Unable to connect to the database. Please check database hostname, login and password.'); $ok = false; } if ($ok && !mysql_select_db($_POST['DB_name'])) { $message[] = ___('Unable to select the database. Please check database name, login and password. Also check if the database exists.'); $errors['select'] = ___('Unable to select the database. Please check database name, login and password. Also check if the database exists.'); $ok = false; } // FIXME check if database exists before? if ($ok && !@mysql_query(sprintf('CREATE TABLE %s_block (id int(11))', $_POST['DB_prefix']))) { $message[] = sprintf(___('Unable to create a table in the database. Please check database name, login and password. Also make sure there are no %s tables with the same prefix yet.'), htmlspecialchars($CPO['product'], ENT_COMPAT, 'ISO-8859-15')); $errors['CREATE'] = sprintf(___('Unable to create a table in the database. Please check database name, login and password. Also make sure there are no %s tables with the same prefix yet.'), htmlspecialchars($CPO['product'], ENT_COMPAT, 'ISO-8859-15')); } else { if ($ok) @mysql_query(sprintf('DROP TABLE %s_block', $_POST['DB_prefix'])); } [[ ********* File: _include/db_mysql.inc.php ********* ]] if (!defined('CPO')) exit; class DB_mysql { var $Host = ""; // mysql-server var $Database = ""; // database-name var $User = ""; // user and password to log into the DB var $Password = ""; var $Link_ID = 0; // result of mysql_connect() var $Query_ID = 0; // result of mysql_query() var $Record = array(); // actual mysql_fetch_array()-result var $Row; // actual row-number var $Errno = 0; // error-status var $Error = ""; // functions // display an error-message and stop function halt($msg) { // check if the database access for logging below failed too. if (isset($GLOBALS['databaseError'])) die('<br />' . 'An error occured while we were handling an error. This is bad.' . '<br />' . $msg); $GLOBALS['databaseError'] = true; $t = '<br />'; $t .= sprintf('<strong>Database error:</strong><br /> %s <br />', htmlspecialchars($msg, ENT_COMPAT, 'ISO-8859-15')); $t .= sprintf('<em>%s (%d)</em><br />', htmlspecialchars($this->Error, ENT_COMPAT, 'ISO-8859-15'), htmlspecialchars($this->Errno, ENT_COMPAT, 'ISO-8859-15')); if ($GLOBALS['varDEBUG']<=0 && $_SESSION['SID_user']['admin']!=ROLE_ADMIN) { $t = 'Sorry, an error occured while processing this request.'; // translation function __() only exists if no fatal error occured if (function_exists('__')) $t = __($t); } print($t); logaction('database error', $this->Error . ' (' . $this->Errno . ')' . '; Request: ' . $_SERVER["REQUEST_URI"] . (!empty($_SERVER['HTTP_REFERER']) ? '; Referer:' . $_SERVER['HTTP_REFERER'] : '; No referer') . '; ' . $msg); die(); } // connect to the database function connect() { if (0 == $this->Link_ID) { $this->Link_ID = @mysql_connect($this->Host, $this->User, $this->Password); if (!$this->Link_ID) { $this->halt('Database connect failed'); } if (!mysql_select_db($this->Database)) { $this->halt('cannot use database ' . $this->Database); } } } // perform a query function query($Query_String) { if ($Query_String=='') return; $this->connect(); // printf("Debug: query = %s<br />\n", $Query_String); $this->Query_ID = mysql_query($Query_String,$this->Link_ID); $this->Row = 0; $this->Errno = mysql_errno(); $this->Error = mysql_error(); if (!$this->Query_ID) { $this->halt("Invalid SQL: ".$Query_String); } if ($GLOBALS['CPODEBUG'] & DEBUG_LOG_QUERIES) $GLOBALS['CPODEBUGLOG'][] = $Query_String; if ($GLOBALS['CPODEBUG'] & DEBUG_COUNT_QUERIES) $GLOBALS['CPODEBUGQUERYCOUNT']++; return $this->Query_ID; } // go to the next Record function next_record() { $this->Record = mysql_fetch_array($this->Query_ID); $this->Row += 1; $this->Errno = mysql_errno(); $this->Error = mysql_error(); $stat = is_array($this->Record); if (!$stat) { mysql_free_result($this->Query_ID); $this->Query_ID = 0; } return $stat; } function next_record_assoc() { $this->Record = mysql_fetch_array($this->Query_ID, MYSQL_ASSOC); $this->Row += 1; $this->Errno = mysql_errno(); $this->Error = mysql_error(); $stat = is_array($this->Record); if (!$stat) { mysql_free_result($this->Query_ID); $this->Query_ID = 0; } return $stat; } // go to the Record at position $pos function seek($pos) { $status = mysql_data_seek($this->Query_ID, $pos); if ($status) $this->Row = $pos; return; } function num_rows() { return mysql_num_rows($this->Query_ID); } function affected_rows() { return mysql_affected_rows($this->Link_ID); } function num_fields() { return mysql_num_fields($this->Query_ID); } function v($Name) { return $this->Record[$Name]; } function p($Name) { print $this->Record[$Name]; } function table_exists($table) { $this->connect(); $tables=mysql_list_tables($this->Database); while (list($temp)=mysql_fetch_array($tables)) { // echo "BALU: $temp<br />"; if($temp == $table) return 1; } return 0; } function record() { return $this->Record; } function lastInsertId() { return mysql_insert_id($this->Query_ID); } }

Code nachher:

[[ ********* File: _setup/step2.php ********* ]] if ($ok && !$mysqli=@mysqli_connect($_POST['DB_hostname'], $_POST['DB_login'], $_POST['DB_password'])) { $message[] = ___('Unable to connect to the database. Please check database hostname, login and password.'); $errors['connect'] = ___('Unable to connect to the database. Please check database hostname, login and password.'); $ok = false; } if ($ok && !mysqli_select_db($mysqli, $_POST['DB_name'])) { $message[] = ___('Unable to select the database. Please check database name, login and password. Also check if the database exists.'); $errors['select'] = ___('Unable to select the database. Please check database name, login and password. Also check if the database exists.'); $ok = false; } // FIXME check if database exists before? if ($ok && !@mysqli_query($mysqli, sprintf('CREATE TABLE %s_block (id int(11))', $_POST['DB_prefix']))) { $message[] = sprintf(___('Unable to create a table in the database. Please check database name, login and password. Also make sure there are no %s tables with the same prefix yet.'), htmlspecialchars($CPO['product'], ENT_COMPAT, 'ISO-8859-15')); $errors['CREATE'] = sprintf(___('Unable to create a table in the database. Please check database name, login and password. Also make sure there are no %s tables with the same prefix yet.'), htmlspecialchars($CPO['product'], ENT_COMPAT, 'ISO-8859-15')); } else { if ($ok) @mysqli_query($mysqli, sprintf('DROP TABLE %s_block', $_POST['DB_prefix'])); } [[ ********* File: _include/db_mysql.inc.php ********* ]] if (!defined('CPO')) exit; class DB_mysql { var $Host = ""; // mysql-server var $Database = ""; // database-name var $User = ""; // user and password to log into the DB var $Password = ""; var $Link_ID = 0; // result of mysqli_connect() var $Query_ID = 0; // result of mysqli_query() var $Record = array(); // actual mysqli_fetch_array()-result var $Row; // actual row-number var $Errno = 0; // error-status var $Error = ""; // functions // display an error-message and stop function halt($msg) { // check if the database access for logging below failed too. if (isset($GLOBALS['databaseError'])) die('<br />' . 'An error occured while we were handling an error. This is bad.' . '<br />' . $msg); $GLOBALS['databaseError'] = true; $t = '<br />'; $t .= sprintf('<strong>Database error:</strong><br /> %s <br />', htmlspecialchars($msg, ENT_COMPAT, 'ISO-8859-15')); $t .= sprintf('<em>%s (%d)</em><br />', htmlspecialchars($this->Error, ENT_COMPAT, 'ISO-8859-15'), htmlspecialchars($this->Errno, ENT_COMPAT, 'ISO-8859-15')); if ($GLOBALS['varDEBUG']<=0 && $_SESSION['SID_user']['admin']!=ROLE_ADMIN) { $t = 'Sorry, an error occured while processing this request.'; // translation function __() only exists if no fatal error occured if (function_exists('__')) $t = __($t); } print($t); logaction('database error', $this->Error . ' (' . $this->Errno . ')' . '; Request: ' . $_SERVER["REQUEST_URI"] . (!empty($_SERVER['HTTP_REFERER']) ? '; Referer:' . $_SERVER['HTTP_REFERER'] : '; No referer') . '; ' . $msg); die(); } // connect to the database function connect() { if (isset($GLOBALS['DB_LINK_ID'])) $this->Link_ID = $GLOBALS['DB_LINK_ID']; if (0 == $this->Link_ID) { $this->Link_ID = @mysqli_connect($this->Host, $this->User, $this->Password); if (!$this->Link_ID) { $this->halt('Database connect failed'); } // force using LATIN1 and return to standard SQL mode // uncomment if necessary /* mysqli_query($this->Link_ID, "SET sql_mode = ''"); mysqli_query($this->Link_ID, "SET character_set_results = 'latin1', character_set_client = 'latin1', character_set_connection = 'latin1', character_set_database = 'latin1', character_set_server = 'latin1'"); mysqli_query($this->Link_ID, "SET NAMES 'latin1'"); mysqli_query($this->Link_ID, "SET CHARACTER SET latin1"); */ if (!mysqli_select_db($this->Link_ID, $this->Database)) { $this->halt('cannot use database ' . $this->Database); } $GLOBALS['DB_LINK_ID'] = $this->Link_ID; } } // perform a query function query($Query_String) { if ($Query_String=='') return; $this->connect(); // printf("Debug: query = %s<br />\n", $Query_String); $this->Query_ID = mysqli_query($this->Link_ID, $Query_String); $this->Row = 0; $this->Errno = mysqli_errno($this->Link_ID); $this->Error = mysqli_error($this->Link_ID); if (!$this->Query_ID) { $this->halt("Invalid SQL: ".$Query_String); } if ($GLOBALS['CPODEBUG'] & DEBUG_LOG_QUERIES) $GLOBALS['CPODEBUGLOG'][] = $Query_String; if ($GLOBALS['CPODEBUG'] & DEBUG_COUNT_QUERIES) $GLOBALS['CPODEBUGQUERYCOUNT']++; return $this->Query_ID; } // go to the next Record function next_record() { $this->Record = mysqli_fetch_array($this->Query_ID); $this->Row += 1; $this->Errno = mysqli_errno($this->Link_ID); $this->Error = mysqli_error($this->Link_ID); $stat = is_array($this->Record); if (!$stat) { mysqli_free_result($this->Query_ID); $this->Query_ID = 0; } return $stat; } function next_record_assoc() { $this->Record = mysqli_fetch_array($this->Query_ID, MYSQLI_ASSOC); $this->Row += 1; $this->Errno = mysqli_errno($this->Link_ID); $this->Error = mysqli_error($this->Link_ID); $stat = is_array($this->Record); if (!$stat) { mysqli_free_result($this->Query_ID); $this->Query_ID = 0; } return $stat; } // go to the Record at position $pos function seek($pos) { $status = mysqli_data_seek($this->Query_ID, $pos); if ($status) $this->Row = $pos; return; } function num_rows() { return mysqli_num_rows($this->Query_ID); } function affected_rows() { return mysqli_affected_rows($this->Link_ID); } function num_fields() { return mysqli_num_fields($this->Query_ID); } function v($Name) { return $this->Record[$Name]; } function p($Name) { print $this->Record[$Name]; } function table_exists($table) { $this->connect(); $tables=mysqli_query($this->Link_ID, "SHOW TABLES"); while (list($temp)=mysqli_fetch_array($tables)) { if($temp == $table) return 1; } return 0; } function record() { return $this->Record; } function lastInsertId() { return mysqli_insert_id($this->Query_ID); } }
« zurück