MySQLi相当于mysql_result()吗? [英] MySQLi equivalent of mysql_result()?
问题描述
我正在将一些旧的PHP代码从mysql移植到MySQLi,但遇到了一个小麻烦.
I'm porting some old PHP code from mysql to MySQLi, and I've ran into a minor snag.
有没有与旧版mysql_result()
功能等效的功能?
Is there no equivalent to the old mysql_result()
function?
我知道mysql_result()
在处理多于1行时比其他函数慢,但是很多时候我只有1个结果和1个字段.使用它,我可以将4行压缩为1.
I know mysql_result()
is slower than the other functions when you're working with more than 1 row, but a lot of the time I have only 1 result and 1 field. Using it lets me condense 4 lines into 1.
旧代码:
if ($r && mysql_num_rows($r))
$blarg = mysql_result($r, 0, 'blah');
所需代码:
if ($r && $r->num_rows)
$blarg = $r->result(0, 'blah');
但是没有这样的事情. :(
But there is no such thing. :(
有什么我想念的吗?还是我必须把它吸干并制作所有东西:
Is there something I'm missing? Or am I going to have to suck it up and make everything:
if ($r && $r->num_rows)
{
$row = $r->fetch_assoc();
$blarg = $row['blah'];
}
推荐答案
PHP 5.4现在支持函数数组取消引用,这意味着您可以执行以下操作:
PHP 5.4 now supports function array dereferencing, which means you can do this:
if ($r && $r->num_rows)
{
$row = $r->fetch_assoc()['blah'];
}
这篇关于MySQLi相当于mysql_result()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!