php MySQLi 相当于 mysql_result()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2089590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQLi equivalent of mysql_result()?
提问by DOOManiac
I'm porting some old PHP code from mysql to MySQLi, and I've ran into a minor snag.
我正在将一些旧的 PHP 代码从 mysql 移植到 MySQLi,但我遇到了一个小问题。
Is there no equivalent to the old mysql_result()function?
没有相当于旧mysql_result()功能的吗?
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.
我知道mysql_result()当您处理超过 1 行时比其他函数慢,但很多时候我只有 1 个结果和 1 个字段。使用它可以让我将 4 行压缩为 1 行。
Old code:
旧代码:
if ($r && mysql_num_rows($r))
$blarg = mysql_result($r, 0, 'blah');
Desired code:
所需代码:
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'];
}
采纳答案by Clamburger
PHP 5.4 now supports function array dereferencing, which means you can do this:
PHP 5.4 现在支持函数数组解引用,这意味着您可以这样做:
if ($r && $r->num_rows)
{
$row = $r->fetch_assoc()['blah'];
}
回答by Mario Lurig
While answered, I thought I could improve on the answer given after having the same question. The following function fully replicates the mysql_result() function, and returns false when you are out-of-bounds on your request (empty result, no row of that number, no column of that number). It does have the added benefit that, if you don't specify the row, it assumes 0,0 (one less value to be passed). The function was updated to allow for the numerical offset of the field or the field name.
在回答时,我认为我可以在遇到相同问题后改进给出的答案。下面的函数完全复制了 mysql_result() 函数,并在您的请求越界时返回 false(空结果,没有该数字的行,没有该数字的列)。它确实有一个额外的好处,如果您不指定行,它将假定为 0,0(要传递的值少一个)。该函数已更新以允许字段或字段名称的数字偏移量。
function mysqli_result($res,$row=0,$col=0){
$numrows = mysqli_num_rows($res);
if ($numrows && $row <= ($numrows-1) && $row >=0){
mysqli_data_seek($res,$row);
$resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res);
if (isset($resrow[$col])){
return $resrow[$col];
}
}
return false;
}
回答by HertzaHaeon
You can do this by fetching an object instead of an array.
你可以通过获取一个对象而不是一个数组来做到这一点。
$mysqli->query("SELECT email FROM users WHERE userid = 'foo'")->fetch_object()->email;
You need PHP 5+ to use method chaining like this.
您需要 PHP 5+ 才能使用这样的方法链。
Alternatively, if you use procedural MySQLi, it's easy to write your own mysqli_resultfunction that matches mysql_result.
或者,如果您使用过程 MySQLi,则很容易编写自己的mysqli_result匹配mysql_result.
回答by Cris
function db_result($result,$row,$field) {
if($result->num_rows==0) return 'unknown';
$result->data_seek($row);
$ceva=$result->fetch_assoc();
$rasp=$ceva[$field];
return $rasp;
}
回答by Dereleased
Well, you can always shorten it to something like this:
好吧,你总是可以把它缩短成这样:
if ($r && $r->num_rows)
list($blarg) = $r->fetch_row();
But that might be as good as you're going to get.
但这可能和你得到的一样好。
回答by nfroidure
I suggest you to add this line to Cris' solutionin order to be able to get a result by both doing db_result('mytable.myfield)and db_result('myfield')since it is the default behavior of the original mysql_resultfunction.
我建议您将此行添加到Cris 的解决方案中,以便能够通过执行db_result('mytable.myfield)和db_result('myfield')因为它是原始mysql_result函数的默认行为来获得结果。
function db_result($result,$row,$field) {
if($result->num_rows==0) return 'unknown';
$result->data_seek($row);
$ceva=$result->fetch_assoc();
return (isset($ceva[$field])?$ceva[$field]
:(strpos($field,'.')?$ceva[substr($field,strrpos($field,'.')+1)]:''));
}
回答by netrox
If you select only ONE field in the query and you only expect a single returned data of a selected field, then this works:
如果您在查询中只选择一个字段,并且您只期望所选字段的单个返回数据,则此方法有效:
function mysqli_datum($result)
{
if ($result->num_rows == 0)
return;
$result->data_seek(0);
$row=$result->fetch_row();
return $row[0];
}
回答by Dima L.
I use the following function to replace mysql_result()
我使用以下函数来替换 mysql_result()
function mysqli_result($result, $iRow, $field = 0)
{
if(!mysqli_data_seek($result, $iRow))
return false;
if(!($row = mysqli_fetch_array($result)))
return false;
if(!array_key_exists($field, $row))
return false;
return $row[$field];
}
回答by Buttle Butkus
Here's an adaptation of Mario Lurig's answer using a mysqli_resultobject instead of the procedural version of mysqli.
这是使用mysqli_result对象而不是mysqli.
/**
* Accepts int column index or column name.
*
* @param mysqli_result $result
* @param int $row
* @param int|string $col
* @return bool
*/
function resultMysqli(mysqli_result $result,$row=0,$col=0) {
//PHP7 $row can use "int" type hint in signature
$row = (int)$row; // PHP5 - cast to int
if(!is_numeric($col) ) { // cast to string or int
$col = (string)$col;
} else {
$col = (int)$col;
}
$numrows = $result->num_rows;
if ($numrows && $row <= ($numrows-1) && $row >=0) {
$result->data_seek($row);
$resrow = (is_numeric($col)) ? $result->fetch_row() : $result->fetch_assoc();
if (isset($resrow[$col])){
return $resrow[$col];
}
}
return false;
}
回答by Fahad
I ended up using a custom function using procedural style:
我最终使用了一个使用程序风格的自定义函数:
function mysqli_result($res, $row, $field=0) {
mysqli_data_seek($res, $row);
return mysqli_fetch_array($res)[$field];
}
Reference: https://www.sitepoint.com/community/t/change-mysql-result-to-mysqli/190972/6
参考:https: //www.sitepoint.com/community/t/change-mysql-result-to-mysqli/190972/6

