Skip to content

Instantly share code, notes, and snippets.

@dkrusky
Created April 18, 2018 09:05
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 dkrusky/fda994fa95a841c613bf209963a271fb to your computer and use it in GitHub Desktop.
Save dkrusky/fda994fa95a841c613bf209963a271fb to your computer and use it in GitHub Desktop.
Wrapper to convert a php site mysql_ procedural to mysqli_ without having to change any code where mysql_ procedural commands are no longer available. (migrating from php 5 to php 7)
<?php
// include this file somewhere that the entire project will have access to it, and disable the old mysql extension.
if(!function_exists('mysql_connect')) {
function mysql_connect($server, $username, $password, $new_link=false) {
return mysqli_connect($server, $username, $password );
}
function mysql_query( $query, $link_identifier ) {
return mysqli_query($link_identifier, $query);
}
function mysql_select_db( $database_name, $link_identifier = NULL ) {
return mysqli_select_db($link_identifier, $database_name);
}
function mysql_real_escape_string($unescaped_string, $link_identifier = NULL) {
return mysqli_real_escape_string( $link_identifier, $unescaped_string );
}
function mysql_fetch_row($result) {
return mysqli_fetch_row($result);
}
function mysql_close($link_identifier = NULL) {
@mysqli_close($link_identifier);
}
function mysql_fetch_assoc($result) {
return mysqli_fetch_assoc($result);
}
function mysql_result($result, $row, $field = 0) {
$numrows = mysqli_num_rows($result);
if ($numrows && $row <= ($numrows-1) && $row >=0){
mysqli_data_seek($result,$row);
$resrow = (is_numeric($field)) ? mysqli_fetch_row($result) : mysqli_fetch_assoc($result);
if (isset($resrow[$field])){
return $resrow[$field];
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment