不建议使用的MySql函数

编程入门 行业动态 更新时间:2024-10-10 08:23:47
本文介绍了不建议使用的MySql函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Php不会在其中推广一些MySql函数即将到来的日子.

Php is not going to promote some MySql functions in upcoming days.

PHP手册中有一些有关清理不赞成使用的函数的示例.但是例如当我在下面的代码中用mysqli_query替换mysql_query时不起作用. Notepad ++也将它们视为自己定义的函数.所有示例均在PHP手册中使用OOP.我需要一个不使用面向对象编程的示例.

There is some examples about cleaning deprecated functions in PHP manual. But for example; when i replace mysql_query with mysqli_query in code below doesn't work. Also Notepad++ treats them like functions which is defined by myself. All examples are using OOP in PHP manual. I need an example without using object orianted programing.

有人可以告诉我如何从过时的mysql函数中清除代码吗?

Can someone tell me that how can i clean my code from deprecated mysql functions?

function db_connect_select() { $connection = @mysql_connect(MYSQL_HOSTNAME, USERNAME_SELECT, PASSWORD); if (!$connection) { return false; } if (!mysql_select_db(DATABASE)) { return false; } mysql_query("SET NAMES UTF8"); return $connection; } function db_result_to_array($result) { $res_array = array(); for ($count = 0; $row = mysql_fetch_array($result); $count++) { $res_array[$count] = $row; } return $res_array; } function select_top_tags() { $connection = db_connect_select(); $query = 'SELECT * FROM top_tags ORDER BY tag_name ASC'; $result = db_result_to_array(mysql_query($query)); if(mysql_ping($connection)) { mysql_close($connection); } return $result; }

推荐答案

这将毫无意义. 单纯的机械替换将无济于事.

It will just make no sense. A mere mechanical replacement will do no good.

您必须了解,它本身不是旧功能,但不鼓励使用旧功能.

You have to understand that it is not old functions themselves, but old ways of using them is discouraged.

因此,如果您想保持当前代码不变-只需保留它即可. 手册中的红色框并不让人害怕,并且这些功能实际上会引发不推荐使用的错误的版本尚未发布. 因此,在遇到任何不便之前,您需要提前3-4年.即使这样,关闭不赞成使用的错误也只需要一个运行时设置即可.

So, if you want to keep your current code as is - just keep it. A red box in the manual is not that scary, and the version in which these functions are actually would raise a deprecated-level error is not out yet. So, you have a 3-4 years ahead, before you will encounter whatever inconvenience. And even then to turn off deprecated-level errors is a matter of one runtime setting.

但是,如果您想编写更好的代码-您必须对 PDO 使用OOP方式 (并且我可以向您保证,OOP并不是 所致.尽管在编写时需要一些知识,但是使用现成的类非常容易.与熟悉的函数的唯一区别是一点点->东西.没什么大不了的

But if you want to write the better code - you have to use OOP way with PDO (and I can assure you that OOP is not that scaring. Although it require some knowledge when writing, it is very easy to use a ready made class. The only difference from familiar functions is a little -> thing. Not a big deal)

所以,你去:

function db_connect_select() { $dsn = 'mysql:host='.MYSQL_HOSTNAME.';dbname='.DATABASE.';charset=utf8'; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); return new PDO($dsn,USERNAME_SELECT, PASSWORD, $opt); } function db_result_to_array($query,) { // not needed with PDO } function select_top_tags() { global $pdo; $query = 'SELECT * FROM top_tags ORDER BY tag_name ASC'; $stm = $pdo->prepare($query); $stm->execute(); return $stm->fetchAll(); }

用法:

$pdo = db_connect_select(); // somewhere in a bootstrap file $tags = select_top_tags();

更多推荐

不建议使用的MySql函数

本文发布于:2023-11-24 23:10:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627271.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   建议   MySql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!