mysqli_result类的对象无法转换为字符串

时间:2015-03-14 13:49:19

标签: php mysql

这是代码。当我执行它时,它显示错误消息"可捕获的致命错误:类mysqli_result的对象无法转换为字符串" 请帮我解决这个问题。

$sql = "SELECT dname FROM bdonor WHERE DID = 2";
$dname = $con->query($sql);
echo $dname;

2 个答案:

答案 0 :(得分:0)

您正在尝试将 mysqli_result转换为字符串(显然它会返回错误),试试这个..

$sql = "SELECT dname FROM bdonor WHERE DID = 2";
$result= $con->query($sql);
while($row = $result->fetch_array(MYSQLI_ASSOC))
       echo $row['dname '].'<br />'; // here is the output display line by line

答案 1 :(得分: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;
}