問題描述
我正在將一些舊的 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()?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!