可捕获的致命错误PHP

时间:2016-03-10 09:32:58

标签: php mysqli

<?php
$adress = 'localhost';
$user = 'root';
$password = '';
$dbname = 'motd';

$link = mysqli_connect($adress, $user, $password, $dbname);
if (!$link) {
    die('Konnte keine Verbindung zur Datenbank herstellen weil wegen: ' . mysql_error());
}
echo 'Erfolgreich mit dem lokalen SQL-Server verbunden';

$sql = $link->query("SELECT message FROM messages WHERE id = '1'");
print(
    "<p>" . $sql . "</p>"
    );



mysqli_close($link);

这段代码给了我错误:

  

捕获致命错误:类mysqli_result的对象无法转换为字符串    第28行的F:\ xampp \ htdocs \ motd.php

任何线索?我不明白为什么!

2 个答案:

答案 0 :(得分:1)

$sql包含您的myqsli_result。您正在尝试打印$sql这是一个无法实现的对象。

来自文档(您可以在mysqli_result上使用这些方法:

mysqli_result::$current_field — Get current field offset of a result pointer
mysqli_result::data_seek — Adjusts the result pointer to an arbitrary row in the result
mysqli_result::fetch_all — Fetches all result rows as an associative array, a numeric array, or both
mysqli_result::fetch_array — Fetch a result row as an associative, a numeric array, or both
mysqli_result::fetch_assoc — Fetch a result row as an associative array
mysqli_result::fetch_field_direct — Fetch meta-data for a single field
mysqli_result::fetch_field — Returns the next field in the result set
mysqli_result::fetch_fields — Returns an array of objects representing the fields in a result set
mysqli_result::fetch_object — Returns the current row of a result set as an object
mysqli_result::fetch_row — Get a result row as an enumerated array
mysqli_result::$field_count — Get the number of fields in a result
mysqli_result::field_seek — Set result pointer to a specified field offset
mysqli_result::free — Frees the memory associated with a result
mysqli_result::$lengths — Returns the lengths of the columns of the current row in the result set
mysqli_result::$num_rows — Gets the number of rows in a result

答案 1 :(得分:1)

mysqli_query

  

失败时返回FALSE。成功的SELECT,SHOW,DESCRIBE或   EXPLAIN查询mysqli_query()将返回一个mysqli_result对象

在你的代码中,你试图回应你有错误的对象

将其用作

$sql = "SELECT message FROM messages WHERE id = '1'";
if ($result = $link->query($sql)) {

    /* fetch object array */
    while ($row = $result->fetch_row()) {
        printf("%s (%s)\n", $row[0]);
    }

    /* free result set */
    $result->close();
}

阅读http://php.net/manual/en/mysqli-result.fetch-object.php