根据以下我用于常规mysql的代码,如何将其转换为使用mysqli?
像更改mysql_query($sql);为一样简单mysqli_query($sql);吗?
mysql_query($sql);
mysqli_query($sql);
<?PHP //in my header file that is included on every page I have this $DB["dbName"] = "emails"; $DB["host"] = "localhost"; $DB["user"] = "root"; $DB["pass"] = ""; $link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>An Internal Error has Occured. Please report following error to the webmaster.<br><br>".mysql_error()."'</center>"); mysql_select_db($DB['dbName']); // end header connection part // function from a functions file that I run a mysql query through in any page. function executeQuery($sql) { $result = mysql_query($sql); if (mysql_error()) { $error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>'; if ($_SESSION['auto_id'] == 1) { $sql_formatted = highlight_string(stripslashes($sql), true); $error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error(); } die($error); } return $result; } // example query ran on anypage of the site using executeQuery function $sql='SELECT auto_id FROM friend_reg_user WHERE auto_id=' .$info['auto_id']; $result_member=executequery($sql); if($line_member=mysql_fetch_array($result_member)){ extract($line_member); } else { header("location: index.php"); exit; } ?>
首先要做的可能是将每个mysql_*函数调用都替换为等效函数mysqli_*,至少在您愿意使用过程式API的情况下-考虑到您已经有一些基于MySQL API的代码,这将是更简单的方法是一种程序性的。
mysql_*
mysqli_*
为了解决这个问题,“ MySQLi扩展功能摘要”绝对是有用的。
例如:
mysql_connect
mysqli_connect
mysql_error
mysqli_error
mysqli_connect_error
mysql_query
mysqli_query
注意:对于某些功能,您可能需要仔细检查参数:也许在这里和那里有一些区别,但是我说的不是很多:mysql和mysqli都基于同一个库 (libmysql;至少对于PHP <= 5.2)
mysql_select_db
mysqli_select_db
完成后,尝试执行脚本的新版本…并检查是否一切正常;如果没有…时间寻找虫子;-)