简体   繁体   English

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

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

I don't know what is going on exactly, but all only the first character is returned for all my columns when I uploaded my website. 我不知道究竟发生了什么,但是当我上传我的网站时,我的所有列都只返回了第一个字符。 It works perfectly fine on a local machine. 它在本地机器上运行得非常好。

I found a similar question here, but I didn't manage to find the answer: 我在这里找到了类似的问题,但我找不到答案:
https://stackoverflow.com/questions/10507848/mysql-query-returns-only-the-first-letter-of-strings-only-when-page-is-viewed-on https://stackoverflow.com/questions/10507848/mysql-query-returns-only-the-first-letter-of-strings-only-when-page-is-viewed-on

    // 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();

This outputs on localhost for example: 这在localhost上输出,例如:

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

But on the live server: 但在实时服务器上:

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

Any ideas? 有任何想法吗?

Edit 编辑

I think that this applies only to string types. 我认为这仅适用于字符串类型。 The integer types return for example: 整数类型返回例如:

int(2893) INT(2893)

I'm assuming that your database or table config is similar to your localhost (better to double check your table). 我假设您的数据库或表配置与您的localhost类似(最好仔细检查您的表)。 I noticed one mistake: 我注意到一个错误:

1. You called store_result() before calling execute() . 1.在调用execute()之前调用了store_result() execute() As per http://php.net/manual/en/mysqli-stmt.store-result.php execute() should be called first. 根据http://php.net/manual/en/mysqli-stmt.store-result.php,应首先调用execute()

See my code this might solve your problem: 看到我的代码,这可能会解决您的问题:

    /* 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();

Let me know if this solved your problem. 如果这解决了您的问题,请告诉我。

Alternatively, you can use the straight forward way since you're not giving any input parameter like WHERE first_name LIKE <input here> to your query: 或者,您可以直接使用,因为您没有向查询提供任何输入参数,例如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>";

As suggested in the question you linked, try the code without unset($stmt) and $stmt = db->stmt_init() Instead, you might use free_result() 正如您链接的问题所示,请尝试不使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM