0

I understand how to fetch a result in mysqli, but how would this example be coded for mysqli?

$t=mysql_query($query);
return (mysql_result($t, 0) === '1') ? true : false; 

The first line is easy, but how do you do the second line?

$t=mysqli_query($mysqli, $query);
return (mysql_result($t, 0) === '1') ? true : false; 

I replaced mysql_result with mysqli_result, and it seemed to work, but my editor doesn't recognise mysqli_result as a part of php.

Just want to check that this is the correct way.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Shaun
  • 2,043
  • 3
  • 27
  • 36
  • The documentation provides alternatives: http://php.net/manual/en/function.mysql-result.php Did you try one? – David Sep 16 '14 at 12:33

2 Answers2

2

Try

$t=mysqli_query($mysqli, $query);
return (mysqli_result($t, 0) === '1') ? true : false; 


function mysqli_result($res, $row, $field=0) { 
    $res->data_seek($row); 
    $datarow = $res->fetch_array(); 
    return $datarow[$field]; 
} 
vijaykumar
  • 4,658
  • 6
  • 37
  • 54
-1

i used this solution but there is one problem!

mysql_result supported table prefixes in resultsets

SELECT a.timestamp, b.timestamp FROM table a, table b

with mysql_result, it was possible to access those results directly using

mysql_result($res,0,"a.timestamp");

while with the mysql_fetch assoc this is not possible, since the first timestamp will be overwritten by the second timestamp. however, its possible to mimic this if you returning the results as numeric and than scan the fields of the result set like this:

function mysqli_result($result, $row, $field) {

    $result->data_seek($row);

    // for no table prefix just go ahead and fetch assoc
    if ( strstr($field, ".") === false ) {
        // Fetch result array
        $data = $result->fetch_assoc();

        return $data[$field];
    }

    // workaround for assoc use and overwriting fieldnames like "a.timestamp" and "b.timestamp"
    list($tablename,$fieldname) = explode(".",$field);
    $resultData = $result->fetch_array(MYSQLI_NUM);

    foreach( $result->fetch_fields() as $index => $fieldData ) {

        if ( $fieldData->table == $tablename && $fieldData->name == $fieldname ) {
            return $resultData[$index];
        }
    }

    return false;
}

now its possible to use this like before with mysqli_result($res,0,"a.timestamp");

easteregg
  • 509
  • 4
  • 9