tags:

views:

232

answers:

3

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? Because I can't find one anywhere in the documentation, here on Stack Overflow, or anywhere else with Google...

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, and I'm lazy.

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'];
}

Thanks for any help you can provide.

+1  A: 

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.

Dereleased
A: 

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 spesific 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.

Donny Kurnia
A: 

public function db_result($result,$row,$field) { if($result->num_rows==0) return 'unknown'; $ceva=$result->fetch_assoc(); $rasp=$ceva[$field]; $result->data_seek(0);
return $rasp; }

Cris