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

时间:2014-05-29 13:12:21

标签: php mysql database

我只是坚持这个简单的查询。

<?php

$message = '';

$db = new MySQLi('localhost','mytestweb','aPassword', 'my_mytestweb');

if($db->connect_error){

    $message = $db->connect_error;

}
else {

    $message = "Connected!!!";

    $sql = "SELECT * FROM avwp_comments";

    $result = $db->query($sql);

    if($db->error){

        $message = $db->error;
    }
}

?>

<!doctype html>
<html>
</head>
<body>
<h1>Query to the database</h1>

<?php
if($message){
    echo "<h2>$message</h2>";    
}
echo "<h2>$result</h2>";
?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

直接来自the Manual

$result = $db->query($sql);

while($row = $result->fetch_array())
{
    var_dump($row);
}

$result->close();

$db->close();

答案 1 :(得分:-1)

试试这个,

<?php

        $con=mysqli_connect('localhost','mytestweb','aPassword','my_mytestweb');
        $msg='0';
    if (mysqli_connect_errno()){
     $msg =  " Failed to connect to MySQL: " . mysqli_connect_error();
     }
       $sql = 'SELECT * FROM avwp_comments';
       $result = mysqli_query($con, $sql);
    $row = mysqli_fetch_array($result)   
   if($row) {}
    else {
    $msg = "Error in Query";
    }
    ?>

    <!doctype html>
    <html>
    </head>
    <body>
    <h1>Query to the database</h1>

    <?php



   if($msg != '0'){

        echo "<h2>$msg</h2>";    
    }
   else{

while($row = mysqli_fetch_array($result)){

         echo  $emp_id = $row['employeeid'];
         echo  $fname = $row['firstname'];

  } 

    ?>
    </body>
    </html>