不建议使用mysql函数,不建议使用的MySql函数

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

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.

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.

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.

But if you want to write the better code - you have to use OOP way with 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)

So, here you go:

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();

}

usage:

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

$tags = select_top_tags();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值