MySQLi 相当于 mysql_result()?

2021-12-25 00:00:00 php mysqli

我正在将一些旧的 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'];  
}

相关文章