MySQL returns 只有字段的第一个字符,但在本地工作正常

MySQL returns only the first character of the fields, but works fine on local

我不知道到底发生了什么,但是当我上传我的网站时,我的所有专栏都只 return 编辑了第一个字符。它在本地机器上工作得很好。

我在这里发现了一个类似的问题,但我没有找到答案:

    // Log Table Query
    unset($stmt);
    $stmt = $db->stmt_init();
    $stmt = $db->prepare( "SELECT * FROM logs ORDER BY `id` DESC" );

    $stmt->store_result();
    $stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
    $stmt->execute();

    while( $stmt->fetch() )
    {
        var_dump($r_message);
        var_dump($r_category);
    }

    $stmt->close();

例如在本地主机上输出:

string(5) "Hello"String(3) "Cow"

但在实时服务器上:

string(1) "H"String(1) "C"

有什么想法吗?

编辑

我认为这仅适用于字符串类型。整数类型 return 例如:

int(2893)

如您链接的问题中所建议,尝试不使用 unset($stmt)$stmt = db->stmt_init() 的代码,您可以使用 free_result()

// Log Table Query
$stmt->free_result(); //Free old results first
$stmt = $db->prepare( "SELECT * FROM logs ORDER BY `id` DESC" );

$stmt->store_result();
$stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
$stmt->execute();

while( $stmt->fetch() )
{
    var_dump($r_message);
    var_dump($r_category);
}

$stmt->close();

我假设您的数据库或 table 配置与您的本地主机相似(最好仔细检查您的 table)。 我注意到一个错误:

1。您在调用 execute() 之前调用了 store_result()。根据 http://php.net/manual/en/mysqli-stmt.store-result.php execute() 应首先调用。

查看我的代码,这可能会解决您的问题:

    /* unsetting doesn't matter you're
    going to overwrite it anyway */
    unset($stmt);

    /* you dont need to initialize $stmt with $db->stmt_init(),
    $db->prepare() method will create it for you */
    $stmt = $db->stmt_init();
    $stmt = $db->prepare("SELECT * FROM logs ORDER BY `id` DESC");

    /* execute the query first before storing
    the result and binding it your variables */
    if (!$stmt->execute()) {
        echo "query execution error";
        exit();
    }

    /* store the result */
    $stmt->store_result();

    /* then bind your variables */
    $stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);

    /* fetch data and display */
    while($stmt->fetch()) {
        var_dump($r_message);
        var_dump($r_category);
    }

    $stmt->close();

如果这解决了您的问题,请告诉我。

或者,您可以使用直接的方式,因为您没有为查询提供任何输入参数,例如 WHERE first_name LIKE <input here>

    $result = $db->query("SELECT * FROM logs ORDER BY `id` DESC");

    if ($result === false) {
        echo "query execution error";
        exit();
    }

    /* You can use either MYSQLI_NUM or MYSQLI_ASSOC os MYSQLI_BOTH
    see php.net for more info */
    echo "<pre>";
    while($line = $result->fetch_array(MYSQLI_NUM)) {
        print_r($line);
        echo "\n";
    }
    echo "</pre>";