可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I\'m porting some old PHP code from mysql to MySQLi, and I\'ve ran into a minor snag.
Is there no equivalent to the old mysql_result()
function?
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.
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\'];
}
回答1:
PHP 5.4 now supports function array dereferencing, which means you can do this:
if ($r && $r->num_rows)
{
$row = $r->fetch_assoc()[\'blah\'];
}
回答2:
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.
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;
}
回答3:
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;
}
回答4:
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.
Alternatively, if you use procedural MySQLi, it\'s easy to write your own mysqli_result
function that matches mysql_result
.
回答5:
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.
回答6:
I suggest you to add this line to Cris\' solution in 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_result
function.
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)]:\'\'));
}
回答7:
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];
}
回答8:
This is a good answer, from http://php.net/manual/es/class.mysqli-result.php
<?php
function mysqli_result($result,$row,$field=0) {
if ($result===false) return false;
if ($row>=mysqli_num_rows($result)) return false;
if (is_string($field) && !(strpos($field,\".\")===false)) {
$t_field=explode(\".\",$field);
$field=-1;
$t_fields=mysqli_fetch_fields($result);
for ($id=0;$id<mysqli_num_fields($result);$id++) {
if ($t_fields[$id]->table==$t_field[0] && $t_fields[$id]->name==$t_field[1]) {
$field=$id;
break;
}
}
if ($field==-1) return false;
}
mysqli_data_seek($result,$row);
$line=mysqli_fetch_array($result);
return isset($line[$field])?$line[$field]:false;
}
?>
回答9:
I use the following function to replace 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];
}
回答10:
If you are looking for a robust library to do database connection, I suggest you use AdoDB. This library can connect to multiple database, and you don\'t have to rewrite your query if you change the database, as long as it doesn\'t contain any specific SQL for a certain database engine. Check this page for a usage sample. Also, if you using PHP5, you can use foreach for iteration.
I hope this will help you convert any old codes to a more robust and cross database code.